listFiles() of File not working on symbolic links?

前端 未结 4 1930
南旧
南旧 2021-02-08 01:51

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

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


        
4条回答
  •  故里飘歌
    2021-02-08 02:24

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

提交回复
热议问题