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

}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!