last modified July 6, 2024
In this article we show how to list files in Java with Files.list.
Files.list returns a lazily populated stream of directory elements. The listing is not recursive.
The elements of the stream are Path objects.
The first example lists the current directory.
Main.java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths;
void main() throws IOException {
var path = Paths.get(".");
try (var files = Files.list(path)) {
files.forEach(System.out::println);
}
}
The dot symbol represents the current working directory. We get the path object with Paths.get.
The following example lists directories in the user’s home directory.
Main.java
import java.io.File; import java.io.IOException; import java.nio.file.Files;
void main() throws IOException {
var homeDir = System.getProperty("user.home");
var path = new File(homeDir).toPath();
try (var files = Files.list(path)) {
files.filter(p -> p.toFile().isDirectory())
.forEach(System.out::println);
}
}
We convert the path object to a File with toFile and call the isDirectory method. The stream is filtered with filter.
The next program lists all PDF files.
Main.java
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Paths;
void main() throws IOException {
var homeDir = System.getProperty("user.home")
+ FileSystems.getDefault().getSeparator() + "Downloads";
try (var files = Files.list(Paths.get(homeDir))) {
files.filter(path -> path.toString().endsWith(".pdf"))
.forEach(System.out::println);
}
}
The program lists PDF files in the Downloads directory. The path object is converted to a string and we call endsWith on the string to check if it ends with pdf extension.
We count the number of PDF files.
Main.java
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Paths;
void main() throws IOException {
var homeDir = System.getProperty("user.home")
+ FileSystems.getDefault().getSeparator() + "Downloads";
try (var files = Files.list(Paths.get(homeDir))) {
var nOfPdfFiles = files.filter(path -> path.toString()
.endsWith(".pdf")).count();
System.out.printf("There are %d PDF files", nOfPdfFiles);
}
}
The number of files is determined with count.
In this article we have used Files.list to list the directory contents.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Java tutorials.