Im trying to read the filenames of a folder an save them in an array list, but i get invisble files names in my array that i dont want(actually i want only to saves the .txt fil
Use java.nio.file. Using Java 8:
final Path dir = Paths.get("/Users/MaxRuizTagle/Desktop/lvl/");
final List textFiles = Files.list(dir)
.filter(path -> !Files.isHidden(path))
.map(path -> path.getFileName().toString())
.filter(s -> s.endsWith(".txt"))
.collect(Collectors.toList());
If Java 7, do the equivalent with Files.newDirectoryStream():
final Path dir = Paths.get("/Users/MaxRuizTagle/Desktop/lvl/");
final DirectoryStream dirstream
= Files.newDirectoryStream(dir, "*.txt");
final List textFiles = new ArrayList<>();
for (final Path entry: dirstream)
if (!Files.isHidden(entry))
textFiles.add(entry.getFileName().toString());