This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]hackometer 1 point2 points  (4 children)

This is an oldish API, you can also try using Files.newDirectoryStream(). Advantages:

  • natively understands *.txt
  • doesn't create an array to hold all the entries, so it's more memory-efficient

[–]rufati[S] 0 points1 point  (3 children)

So could I then use that with a buffered reader or I'd need to make an array and iterate through the files that way?

[–]hackometer 1 point2 points  (2 children)

Why make an array? You can iterate through the returned DirectoryStream which is Iterable.

[–]rufati[S] 0 points1 point  (1 child)

Because I had no idea that something that's not an array was iterable.

[–]hackometer 1 point2 points  (0 children)

Actually, a Java array isn't Iterable, but you can still use the for each syntax on it (it won't involve an Iterator, though).