How To Count Files In A Directory Using Java

This example shows how to count the number of files in a folder. File class defines a method list() which returns the array of file names in the directory. By getting the length of that array would mean the number of files in the folder.

Lets look at the simple example.



 * Count Files in a Directory Example
 * @author Krishna
public class CountFilesExample {

	public static void main(String[] args) {
		int count = new File("D:\\").list().length;
		System.out.println("Number of file : " + count);


Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!