Monday, 18 April 2011

How to display all the directories in a directory?

This function displays directories in a directory using filter.
public static void displayAllDir(String dirName) {
File dir = new File(dirName); //eg. C:
File[] files = dir.listFiles();
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);
System.out.println(files.length);
if (files.length == 0) {
System.out.println("Either dir does not exist
or is not a directory");
}
else {
for (int i=0; i< files.length; i++) {
File filename = files[i];
System.out.println(filename.toString());
}
}
}

No comments:

Post a Comment