代码之家  ›  专栏  ›  技术社区  ›  Aly

文件以所有权限存在时发生FileNotFoundException

  •  10
  • Aly  · 技术社区  · 16 年前

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

    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)
    

    但是,该文件确实存在,为了再次检查,我授予了它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
    

    你知道为什么我会得到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();    
      }
    

    属性文件读取:

    raise_ratio=4
    

    在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");
            }
    

    输出如下:

    file doesn't exist
    /homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties
    
    4 回复  |  直到 15 年前
        1
  •  21
  •   beny23    16 年前

    FileNotFoundException: ...Configuration.properties  (No such file or directory)
    --------------------------------------------------^^
    

    这将向我表明文件名可能有一个尾随空格。是否可以通过以下方式重新检查pathToConfiguration变量:

    System.out.println("[" + pathToConfiguration + "]");
    

    再次检查路径是否与您所想的一致?

        2
  •  0
  •   lorenzog    16 年前

    是否有一些模糊的NFS/文件服务器挂载只对登录shell有效,而对应用程序无效?

    尝试在$HOME中复制文件,看看是否有效。

        3
  •  0
  •   Geo    16 年前

    如果你这样写,会输出什么:

    System.out.println(new File(".").getAbsolutePath());

    你现在的目录是什么?

        4
  •  0
  •   Thimmayya    16 年前

    编辑 :尝试将文件复制到运行程序的目录,然后查看它是否能够读取。将文件复制到执行目录后,还可以尝试以下操作:

    InputStream in = getClass().getResourceAsStream("/gameTheoryAgentConfiguration.properties");
    configuration.load(in);
    

    (假设类路径中有“.”)