Tuesday, September 26, 2006

Java - File.listFiles() returns null for temporary folders

To find all the files in a given directory we use the method listFiles() in the File class.
It will return all the files and sub directories in the given File, if its a directory and null otherwise.

File dir = new File("/home/user/");
File [] filesAndFolders = dir.listFiles();

This works fine for all the folders in the system except for the temporary folders, such as a temporarily unzipped archive. For example, if u try to list all the files of a deployed web application, which is actually maintained by the container as a temporary folder for deployed web archives, File.listFiles() will return null.

Today i found this while trying to read all the jar files in the WEB-INF/lib directory of a deployed web application.

No comments: