2010-02-22 59 views
10

我试图读取文件和错误我得到的是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) 

但该文件确实存在,只是仔细检查,我给它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 

这在窗口测试(带有一个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"); 
     } 

输出如下:

file doesn't exist 
/homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties 
+1

你能粘贴实际的Java代码吗? – Martin 2010-02-22 18:03:16

+0

抛出异常时,你在运行什么代码?如果使用file.exists()会发生什么? file.getAbsolutePath()? file.canRead /写/执行()? – Pops 2010-02-22 18:04:37

+0

你是否在文件存在的同一台机器上运行你的java代码? – 2010-02-22 18:09:10

回答

19

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

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

这表明,我认为该文件名可能有一个尾随空格。你可以再次检查你的pathToConfiguration变量:

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

要仔细检查路径是你认为它是什么?

+0

让我知道如果这只是一个错字,如果是的话,我会删除这个答案。 – beny23 2010-02-22 18:53:45

+0

@beny:即使这个结果在这个特殊情况下是错误的,好主意。 – Pops 2010-02-22 18:57:03

+2

@beny - 老鹰的眼睛在那里...... – Thimmayya 2010-02-22 18:58:07

0

当你执行你的Java程序上运行它作为相同的“用户”作为当你运行命令行检查?

编辑:尝试将文件复制到您运行程序的目录并查看它是否能够读取它。您也可以尝试以下方法将文件复制到你的执行目录后:

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

(假设你在你的类路径“”)

+0

是的,作为用户at1106 – Aly 2010-02-22 18:20:13

+0

Aly,你能确认System.getProperty(“user.name”)产生at1106吗? – 2010-02-22 18:39:02

0

我想你双重检查的路径超过一次,并作为你说你正在代码所在的同一台机器上运行应用程序。

难道是有一些模糊的NFS /文件服务器挂载仅适用于登录shell,但不适用于应用程序?

尝试复制$ HOME中的文件并查看它是否有效。

+0

我设法将文件复制到$ HOME – Aly 2010-02-22 18:27:06

+0

和...?它工作吗? – lorenzog 2010-02-22 21:11:32

0

,如果你写这什么获取输出:

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

什么是你的当前目录?

+0

这个: /homes/at1106/fourthYearComputing/Induvidual-Project/svn-workspace/trunk/Induvidual_Project /。 – Aly 2010-02-22 18:39:48

+0

您的文件是否存在于该文件夹中? – Geo 2010-02-22 19:14:55

+0

不,但当我创建文件我这样做的绝对路径 – Aly 2010-02-22 23:33:23

相关问题