eradicus eradicus - 1 year ago 79
Java Question

listFiles() of File not working on symbolic links?

I have the following File object pointing to a directory via symbolic link,

File directory = new File("/path/symlink/foo/bar");
String[] files = directory.listFiles();

listFiles() returns null, is this because of the symlink? if yes, how will I go about this if I really want to list the files in bar using the path that contains a symlink?

Answer Source

According to what I've seen while Googling this puzzling behavior, Java requires that you call .getCanonicalFile() on a File whose path contains a link before you can use it in other file operations.


File directory = new File("/path/symlink/foo/bar").getCanonicalFile();
String[] files = directory.listFiles();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download