JB Header
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 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

idea icon 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.
Let us now look at the two approaches for getting count of files in a directory starting with 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);
  }
}
 OUTPUT of the above code
File Count: 3
Explanation of the code
  • In the main() method of DirFileCountWithPath class above, first a new java.io.File instance, named directory, is created which holds a reference to the directory named Level1.
  • list() method is invoked on directory, which returns a String array containing the names of files(and directories) contained in Level1 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 as 3.
Approach 2 - Using NIO’s Path and DirectoryStream to get count of files in a directory
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());
  }
}
 OUTPUT of the above code
File Count: 3
Explanation of the code
  • In the main() method of DirFileCountWithPath class, first an empty ArrayList, named fileNames, is defined and initialized.
  • Next, a DirectoryStream instance is created for Level1 directory using Files.newDirectoryStream() method, which uses Paths.get() method to get a handle to the Level1 directory.
  • The Path objects in directoryStream are then iterated over using a for-each loop, and for each file in the directory, file's name is added to the fileNames arraylist.
  • The size of the arraylist fileNames then gives us the required count of files in the directory Level1 - i.e. 3.
Advantages of NIO(Path & DirectoryStream) based approach over java.io.File based approach It is recommended to use NIO based approach, which uses 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 the File.list() based approach.
  • DirectoryStream based approach is more responsive when working with remote directories, as compared to the File.list().