Java File.isDirectory() 为 Linux 中的目录返回 False
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2450612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java File.isDirectory() returns False for a Directory in Linux
提问by shelt536
Please see code snippet:
请看代码片段:
File[] additionalFiles = new File(FILE_PATH).listFiles();
boolean isDirectory = file.isDirectory();
I have verified that the directory path is correct, and when I run the code on Windows, the value of isDirectory is true (as it should be). Any suggestions as to why this occurs on Linux (RedHat Enterprise Linux)?
我已经验证目录路径是正确的,并且当我在 Windows 上运行代码时,isDirectory 的值为 true(应该是)。关于为什么在 Linux (RedHat Enterprise Linux) 上会发生这种情况的任何建议?
回答by Yishai
Symlinks don't read as directories, if I remember correctly. The right way around that is:
如果我没记错的话,符号链接不会读作目录。正确的方法是:
new File(FILE_PATH).getCanonicalFile().isDirectory();
(NOTE: Untested, I don't have a linux box to test this on easily).
(注意:未经测试,我没有可以轻松测试的 linux 机器)。
回答by JavaMeat
I experienced this issue once. My case is so funny, I was reading the path from a properties file and that path contained a tab character at the end of the string. That was the reason why the path wasn't recognized as a directory
我曾经遇到过这个问题。我的情况很有趣,我正在从属性文件中读取路径,并且该路径在字符串的末尾包含一个制表符。这就是路径不被识别为目录的原因
回答by Fazal
Checkout this link http://bugs.sun.com/view_bug.do;jsessionid=56e03cb783aaf9725daf5ec8d8?bug_id=6539692
查看此链接 http://bugs.sun.com/view_bug.do;jsessionid=56e03cb783aaf9725daf5ec8d8?bug_id=6539692
You may have this issue.
你可能有这个问题。
Otherwise I would guess an issue with file permissions (though that might throw back security exception and I am assuming your code does not wrap it and return false) or may be a sym link issue that I dont know much about.
否则我会猜测文件权限问题(尽管这可能会抛出安全异常,我假设您的代码没有包装它并返回 false)或者可能是我不太了解的符号链接问题。

