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.

CountFilesExample.java

package javabeat.net.io;

import java.io.File;

/**
 * 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);
	}

}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*