Java FileNotFoundException 当文件以所有权限存在时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312924/
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
FileNotFoundException when file exists with all permissions
提问by Aly
I am trying to read a file and the error i get is
我正在尝试读取文件,但得到的错误是
java.io.FileNotFoundException: /homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at game.player.gametheoryagent.GameTheoryAgent.<init>(GameTheoryAgent.java:67)
at simulation.Simulator.createPlayer(Simulator.java:141)
at simulation.Simulator.main(Simulator.java:64)
however the file does exist and just to double check i gave it 777 permissions, as shown below:
但是该文件确实存在,只是为了仔细检查我给了它 777 权限,如下所示:
tui% cd /homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations
tui% ls -al
total 4
drwxrwxrwx 3 at1106 cs4 1024 2010-02-22 17:45 .
drwxrwxrwx 4 at1106 cs4 1024 2010-02-22 17:27 ..
-rwxrwxrwx 1 at1106 cs4 260 2010-02-22 17:31 gameTheoryAgentConfiguration.properties
drwxrwxrwx 6 at1106 cs4 1024 2010-02-22 17:41 .svn
Any ideas as to why I'm getting the FNF exception?
关于为什么我得到 FNF 例外的任何想法?
Thanks
谢谢
java code that makes the call:
进行调用的java代码:
File file = new File(pathToConfiguration)
Properties configuration = new Properties();
try{
configuration.load(new FileInputStream(file));
int RAISE_RATIO = Integer.parseInt(configuration.getProperty("raise_ratio"));
}
catch(IOException event){
System.err.println("Error in reading configuration file " + pathToConfiguration);
event.printStackTrace();
}
The properties file reads:
属性文件如下:
raise_ratio=4
This was tested in windows (with a diff pathToConfiguration (which is passed into the constructor)) and works fine.
这在 Windows 中进行了测试(使用 diff pathToConfiguration(传递到构造函数))并且工作正常。
Added in the following checks in the Catch block
在 Catch 块中添加了以下检查
if(file.exists()){
System.out.println("file exists");
}
else{
System.out.println("file doesn't exist");
}
System.out.println(file.getAbsolutePath());
if(file.canRead()){
System.out.println("can read");
}
if(file.canWrite()){
System.out.println("can write");
}
the output is as follows:
输出如下:
file doesn't exist
/homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties
采纳答案by beny23
According to the initial stacktrace there appear to be two spaces between the file name and reason:
根据初始堆栈跟踪,文件名和原因之间似乎有两个空格:
FileNotFoundException: ...Configuration.properties (No such file or directory)
--------------------------------------------------^^
This would indicate to me that the filename possibly has a trailing space. Can you double check your pathToConfiguration variable by:
这将向我表明文件名可能有尾随空格。您能否通过以下方式仔细检查您的 pathToConfiguration 变量:
System.out.println("[" + pathToConfiguration + "]");
To double check that the path is what you think it is?
要仔细检查路径是否是您认为的那样?
回答by Thimmayya
When you execute your java program are you running it as the same 'user' as when you run the command-line checks?
当您执行 Java 程序时,您是否以与运行命令行检查时相同的“用户”身份运行它?
EDIT: Try copying the file to the directory where you run your program from and see if it is able to read it. You can also try the following after copying the file to your execution directory:
编辑:尝试将文件复制到您运行程序的目录,看看它是否能够读取它。您也可以在将文件复制到您的执行目录后尝试以下操作:
InputStream in = getClass().getResourceAsStream("/gameTheoryAgentConfiguration.properties");
configuration.load(in);
(assuming you have "." in your classpath)
(假设您的类路径中有“.”)
回答by lorenzog
I suppose you double checked the pathname more than once, and as you say you are running the app on the same machine where the code resides.
我想您不止一次地仔细检查了路径名,正如您所说,您正在代码所在的同一台机器上运行该应用程序。
Could it be that there are some obscure NFS/file server mounts that are valid only for the login shell but not for the applications?
是否有一些模糊的 NFS/文件服务器挂载仅对登录 shell 有效,但对应用程序无效?
Try copying the file in your $HOME and see if it works.
尝试复制 $HOME 中的文件,看看它是否有效。
回答by Geo
What gets outputted if you write this:
如果你这样写,会输出什么:
System.out.println(new File(".").getAbsolutePath());
System.out.println(new File(".").getAbsolutePath());
what is your current directory?
你当前的目录是什么?