Pages

Get a number of files in a directory

How to get a number of files in a directory?
  • In java, we can use a class file, i.e., File.java which is available in java.io package.
  • list() method in File.java will get the total number of files in a directory.

Demo on getting no. of files in a directory:

Let us say we have 5 different types of files like Excel, word, pub, txt, zip files in a directory.

Directory Files Count
Directory Files Count

import java.io.File;

public class DirectoryFilesCount {
    public static void main(String args[]) {
        String directorypath = "D:\\path\\Files";
        File directory = new File(directorypath);
        int fileCount = directory.list().length;
        System.out.println("Total number of Files in directory==" + fileCount);
    }
}
Output:
Total number of Files in directory==5
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.