Write a program to get list of all files from a given directory?

import java.io.File;

public class FilesFromDirectory {
	public static void main(String a[]) {
		File file = new File("C:/MyDir/");
		
		File[] allFiles = file.listFiles();
		
		System.out.println("Total files in this directory : " + allFiles.length);
		
		for (File f : allFiles) {
			System.out.println(f.getName());
		}
	}
}
Output
Total files in this directory : 4 file1.txt file2.txt file3.pdf image1.png










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: