The documentation of
listFiles()
mentions that it will returnnull
if this abstract pathname does not denote a directory, or if an I/O error occurs. Additionally, you would need to check withfile.canRead()
whether the application can read the directory.
IMHO
Always use it this way;
String[] files = file.list();
if (files!=null) {
for (String f : files) processFile(f);
}
Recommend this;
File directory = new File(directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
if(fList != null){
for (File file : fList){
if (file.isFile()){
System.out.println(file.getName());
}
}
}
Do let me know if you have any questions.