views:

87

answers:

2

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?

A: 

You could read the Symbolic LINK

al nik
That only works in the (beta) Java 7.
R. Bemrose
+1  A: 

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.

So:

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