Why did you not try it? Next time do make an effort and share where are you stuck.
Using recursion:
import java.io.*;
public class CountFilesInDirectory {
public static void main(String[] args) {
File f = new File("C:/TestFolder");
int count = 0;
for (File file : f.listFiles()) {
if (file.isFile()) {
count++;
}
}
System.out.println("Number of files: " + count);
}
}
Alternate: A simpler one,
new File("/path/to/folder").listFiles().length
Refer:
http://docs.oracle.com/javase/6/docs/api/java/io/File.html[
^]