eradicus eradicus - 6 months ago 44
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?


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();