Wednesday, August 18, 2010

Recursively listing all the files in directory java

/**
* Get all the files in a directory. This recursively gets the files from
* sub directories as well
*
* @param parentDir
* @return
* @throws FileNotFoundException
*/
private static List getFiles(File parentDir)
throws FileNotFoundException {
List result = new ArrayList();
File[] filesAndDirs = parentDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
result.add(file);
if (file.isFile() == false) {
List deeperList = getFiles(file);
result.addAll(deeperList);
}
}
return result;
}

No comments:

Post a Comment

Followers