Learn to use Java 8 APIs along with Files.list()
and DirectoryStream
to list all files present in a directory, including hidden files, recursively.
1. List all files and sub-directories using Files.list()
Files.list()
method to list all file names and sub-directories in current directory.
Files.list(Paths.get(".")) .forEach(System.out::println); Output: .\filename1.txt .\directory1 .\filename2.txt .\Employee.java
2. List only files inside directory using filter expression
You can use filters to filter out sub-directories and print only file names, if you need it.
Files.list(Paths.get(".")) .filter(Files::isRegularFile) .forEach(System.out::println); Output: .\filename1.txt .\filename2.txt .\Employee.java
To list files in a different directory, we can replace "."
with the full path of the directory we desire.
3. List files and sub-directories with Files.newDirectoryStream()
Java provides a more flexible way of traversing a directory content using Files.newDirectoryStream()
.
Please note that if we’re working with a large directory, then using
DirectoryStream
actually make code faster.
Files.newDirectoryStream(Paths.get(".")) .forEach(System.out::println); Output: .\filename1.txt .\directory1 .\filename2.txt .\Employee.java
4. List only files with Files.newDirectoryStream()
To list out only files and excluding all directories from stream, use path filter as second argument.
Files.newDirectoryStream(Paths.get("."), path -> path.toFile().isFile()) .forEach(System.out::println); Output: .\filename1.txt .\filename2.txt .\Employee.java
5. List files of certain extention with Files.newDirectoryStream()
You can change the path filter expression passed in second argument to get files of certain extension only.
Files.newDirectoryStream(Paths.get("."), path -> path.toString().endsWith(".java")) .forEach(System.out::println); Output: .\Employee.java
6. Find all hidden files in directory
To find all hidden files, you can use filter expression file -> file.isHidden()
in any of above example.
Or you can use this shortcut method.
final File[] files = new File(".").listFiles(file -> file.isHidden()); //or final File[] files = new File(".").listFiles(File::isHidden);
In above examples, we learn to use java 8 APIs list or iterate files in directory recursively on various search criteria. Feel free to modify the code and play with it.
Happy Learning !!
References:
DirectoryStream
Files.list() method
ReddiSeharReddy
Hi,
how to apply filter to get the list of files created between two dates.
Could you please suggest on this.
ReddiSeharReddy
Files.newDirectoryStream(Paths.get(directory),
path -> path.toFile().lastModified() > sd.getTime() && path.toFile().lastModified() < ed.getTime());
Rich K
Alex B, that code does not demonstrate what list the filenames go to. Where is the list variable ?
Anubhav Gupta
Here is the code for reading filename and store them in list.
jack
I appreciate the post.
But the code in “Find all hidden files in directory” section gives syntax error.
And, its sad that you didn’t tell how to capture the file names in a list or something else (will be helpful for the people who are learning java 8). In real world, we don’t need to read the file names just to sysout.
Anubhav Gupta
Here is the code for reading file names and store them in list.
Alex B
You’re actually better off doing something like this:
Which allows you to use the streams api to transform/filter the list before you collect it.
sekaijin
Dhiraj Kumar
nice code