文件存在时具有所有权限的FileNotFoundException

Aly*_*Aly 10 java exception filenotfoundexception

我试图读取文件,我得到的错误是

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)
Run Code Online (Sandbox Code Playgroud)

但是文件确实存在,只是为了仔细检查我给它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
Run Code Online (Sandbox Code Playgroud)

关于为什么我得到FNF例外的任何想法?

谢谢

调用的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();    
  }
Run Code Online (Sandbox Code Playgroud)

属性文件如下:

raise_ratio=4
Run Code Online (Sandbox Code Playgroud)

这是在Windows中测试的(使用diff pathToConfiguration(传递给构造函数))并且工作正常.

在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");
        }
Run Code Online (Sandbox Code Playgroud)

输出如下:

file doesn't exist
/homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties
Run Code Online (Sandbox Code Playgroud)

ben*_*y23 21

根据初始堆栈跟踪,文件名和原因之间似乎有两个空格:

FileNotFoundException: ...Configuration.properties  (No such file or directory)
--------------------------------------------------^^
Run Code Online (Sandbox Code Playgroud)

这将向我表明文件名可能有一个尾随空格.你能通过以下方式仔细检查你的pathToConfiguration变量:

System.out.println("[" + pathToConfiguration + "]");
Run Code Online (Sandbox Code Playgroud)

仔细检查路径是否符合您的想法?

  • @beny - 鹰眼睛那里...... (2认同)