In this quick article, you'll learn how to list all files in directory using Java. We will look at different ways to find out all the files available in a certain directory.
Using Files.walk()
Method
In Java 8 and higher, you can use the NIO API Files.walk()
static method to list all files and sub-directories in a certain directory. This method throws a NoSuchFileException
exception if the folder doesn't exist.
Here is an example that list all files and sub-directories in a directory called ~/java-runner
:
try (Stream<Path> paths = Files.walk(Paths.get("/home/attacomsian/java-runner"))) {
// print all files and folders
paths.forEach(System.out::println);
} catch (IOException ex) {
ex.printStackTrace();
}
Since it returns a Stream, you can easily filter-out nested directories and only list regular files like below:
try (Stream<Path> paths = Files.walk(Paths.get("/home/attacomsian/java-runner"))) {
// filer out sub-directories
List<String> files = paths.filter(x -> Files.isRegularFile(x))
.map(Path::toString)
.collect(Collectors.toList());
// print all files
files.forEach(System.out::println);
} catch (IOException ex) {
ex.printStackTrace();
}
Similarly, you can also list only sub-directories by filtering-out all regular files:
try (Stream<Path> paths = Files.walk(Paths.get("/home/attacomsian/java-runner"))) {
// filer out regular files
List<String> folders = paths.filter(x -> Files.isDirectory(x))
.map(Path::toString)
.collect(Collectors.toList());
// print all folders
folders.forEach(System.out::println);
} catch (IOException ex) {
ex.printStackTrace();
}
Here is another example that list all .java
files using Files.walk()
:
try (Stream<Path> paths = Files.walk(Paths.get("/home/attacomsian/java-runner"))) {
// keep only `.java` files
List<String> javaFiles = paths.map(Path::toString)
.filter(x -> x.endsWith(".java"))
.collect(Collectors.toList());
// print all files
javaFiles.forEach(System.out::println);
} catch (IOException ex) {
ex.printStackTrace();
}
By default, the Stream returned by Files.walk()
recursively walks through the file tree up to an n-level (all nested files and folders). However, you can pass another parameter to Files.walk()
to limit the maximum number of directory levels to visit.
Here is an example that restricts the directory level to given folder only (level 1):
try (Stream<Path> paths = Files.walk(Paths.get("/home/attacomsian/java-runner"), 1)) {
// print all files and folders in current folder
paths.forEach(System.out::println);
} catch (IOException ex) {
ex.printStackTrace();
}
In the above code, the second parameter of Fils.walk()
is the maximum number of levels of directories to visit. A value of 0 means that only the starting file is visited, unless denied by the security manager. A value of Integer.MAX_VALUE
may be used to indicate that all levels should be visited.
Using File.listFiles()
Method
In old Java versions (JDK 6 and below), you can use the File.listFiles()
method to get all files and nested folders in a directory.
Here is an example that uses File.listFiles()
to print all files and folders in the given directory:
File folder = new File("/home/attacomsian/java-runner");
// list all files
for (File file : folder.listFiles()) {
System.out.println(file);
}
To list only regular files, do the following:
File folder = new File("/home/attacomsian/java-runner");
// list all regular files
for (File file : folder.listFiles()) {
if (file.isFile()) {
System.out.println(file);
}
}
Similarly, to list only sub-folders, you can use the below code:
File folder = new File("/home/attacomsian/java-runner");
// list all sub-directories
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
System.out.println(file);
}
}
To recursively list all files and folders inside a certain folder, we need to write a recursive function as follows:
public void listFilesRecursive(File folder) {
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
// uncomment this to list folders too
// System.out.println(file);
listFilesRecursive(file);
} else {
System.out.println(file);
}
}
}
Here is how you can call the above recursive function:
File folder = new File("/home/attacomsian/java-runner");
listFilesRecursive(folder);
Further Reading
You may be interested in other Java I/O articles:
- Reading and Writing Files in Java
- How to Read and Write Text Files in Java
- How to Read and Write Binary Files in Java
- Reading and Writing Files using Java NIO API
- How to read a file line by line in Java
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.