listFiles()的文件无法处理符号链接? [英] listFiles() of File not working on symbolic links?

查看:143
本文介绍了listFiles()的文件无法处理符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下File对象通过符号链接指向目录,

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()返回null,这是因为符号链接?如果是的话,如果我真的想使用包含符号链接的路径在列表中列出文件,我将如何解决这个问题?

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?

推荐答案

根据我在谷歌搜索这个令人费解的行为时所看到的,Java要求你在文件 .getCanonicalFile() $ c>其路径包含一个链接,然后才能在其他文件操作中使用它。

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

这篇关于listFiles()的文件无法处理符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆