How to count number of files in a directory in Java | File.list() vs NIO's DirectoryStream
This quick code tip how to count number of files in a directory in Java with code examples. It shows two approaches to code for the problem. First approach uses
The directory for which we will determine the file count is
Let us now look at the two approaches for getting count of files in a directory starting with
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
java.io.File
class, while the second approach uses java.nio.file.Path
and java.nio.file.DirectoryStream
classes. Lastly, it compares the two approaches of getting file counts and suggests the better one.The directory for which we will determine the file count is
C:\JavaBrahman\Level1
. It has the following structure - shown using the DOS command - DIR /s /b
- C:\JavaBrahman\Level1>DIR /s /b C:\JavaBrahman\Level1\file1.txt C:\JavaBrahman\Level1\file2.txt C:\JavaBrahman\Level1\Level2 C:\JavaBrahman\Level1\Level2\file3.txt C:\JavaBrahman\Level1\Level2\file4.txt
How does Java treat files & directories
At a high level, Java considers both files and directories as files when working with file systems, similar to the way UNIX approaches files & directory, i.e. "Everything is a File". We can however distinguish between the two "File" types of files and directories using
java.io.File
’s isDirectory()
method, OR, using NIO’s java.nio.file.Files.isDirectory()
method.
File
, and then moving on to Path
with DirectoryStream
.
Approach 1 - Using java.io’s File.list() to get count of files in a directory
Using java.io’s File.list() to get count of files in a directory
package com.javabrahman.corejava;
import java.io.File;
public class DirFileCounter {
public static void main(String args[]){
File directory=new File("C:\\JavaBrahman\\Level1");
int fileCount=directory.list().length;
System.out.println("File Count:"+fileCount);
}
}
File Count: 3
- In the
main()
method ofDirFileCountWithPath
class above, first a newjava.io.File
instance, nameddirectory
, is created which holds a reference to the directory namedLevel1
. list()
method is invoked ondirectory
, which returns aString
array containing the names of files(and directories) contained inLevel1
directory.- Lastly, the length of array returned by
list()
method gives us the count of file names in the array, which is the desired file count we require. The value of file count obtained is then correctly printed as3
.
Using NIO’s Path and DirectoryStream to get count of files in a directory
package com.javabrahman.corejava;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class DirFileCountWithPath {
public static void main(String args[]) {
List<String> fileNames = new ArrayList<>();
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("C:\\JavaBrahman\\Level1"));
for (Path path : directoryStream) {
fileNames.add(path.toString());
}
} catch (IOException ex) {
}
System.out.println("File Count:"+fileNames.size());
}
}
File Count: 3
- In the
main()
method ofDirFileCountWithPath
class, first an emptyArrayList
, namedfileNames
, is defined and initialized. - Next, a
DirectoryStream
instance is created forLevel1
directory usingFiles.newDirectoryStream()
method, which usesPaths.get()
method to get a handle to theLevel1
directory. - The
Path
objects indirectoryStream
are then iterated over using a for-each loop, and for each file in the directory, file's name is added to thefileNames
arraylist. - The size of the arraylist
fileNames
then gives us the required count of files in the directoryLevel1
- i.e.3
.
Path
and DirectoryStream
, to get the count of files in a directory due to the following reasons-
DirectoryStream
based approach uses less resources when working with large directories, as compared to theFile.list()
based approach.DirectoryStream
based approach is more responsive when working with remote directories, as compared to theFile.list()
.