Hi,
Today I am going to tell you the source code for extracting the each and every details of the files and folder of a given folder. Like in C:\ drive contains many folder as well as files; the folder in turn contains the files too. The below program will list out the details of all that files and folder.
Note: If you try to do it with C:\ drive, it might take longer time to complete the process as for most of the cases, it will contain Program Files, which itself contains thousands of the files and folders.
The source code as follows:
import java.io.File;
public class FileDetails {
FileDetails() {
}
private static void getFileDetails(File folder) {
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File under " + folder + " -> " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory under " + folder + " -> " + listOfFiles[i].getName());
getFileDetails(listOfFiles[i]); // Recursive call of the directory to find out the files under that
}
}
}
public static void main(String[] args) {
FileDetails fileDetails = new FileDetails();
String directory = new String("C://");
File folder = new File(directory);
fileDetails.getFileDetails(folder);
}
}
No comments:
Post a Comment