Friday, September 21, 2007

List the names of all files in a particular directory

This code reads and prints the name of all the files and folders in a particular parent folder.
import java.io.File;

public class DirectoryReader {

public static void main(String[] args) {

File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
}

No comments: