Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException when file exists with all permissions [duplicate]

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:

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?

Thanks

java code that makes the call:

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.

Added in the following checks in the Catch block

        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
like image 450
Aly Avatar asked Feb 22 '10 17:02

Aly


People also ask

How do I fix FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

How do I stop FileNotFoundException in java?

How to avoid FileNotFoundException in java? Please ensure file exists when you try to read from file from path exists (using FileInputStream) to avoid FileNotFoundException. Please ensure file exists when we try to acces file from invalid path using RandomAccessFile to avoid FileNotFoundException.

In which cases FileNotFoundException at compile time will be thrown?

FileNotFoundException is a checked exception is used that occurs when a file path specified for accessing does not exist or is inaccessible. With the checked exception, it means that the java compiler checks at compile time if this exception has been handled or not; otherwise, a compile-time error occurs.


1 Answers

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:

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

To double check that the path is what you think it is?

like image 142
beny23 Avatar answered Nov 08 '22 15:11

beny23