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