Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

public StormAnalysis(){
    try {       
        fScanner = new Scanner(new File("tracks1949to2010_epa.txt"));
        while(fScanner.hasNextLine()){
            System.out.println(fScanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found. Try placing the tracks1949to2010_epa.txt in the same folder as StormAnalysis.java");    
        e.printStackTrace();
    }

}

The above is my code (and I also have an image of the error : http://folk.uio.no/arnabkd/test/images/error-code-task.jpg

As you can see, the txt file is in the same folder as the StormAnalysis.java file. In addition, the code works if I change the file path to "weather.dat" (which was given as another task/problem).

Any ideas will be appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
468 views
Welcome To Ask or Share your Answers For Others

1 Answer

The file isn't there. If it was it wouldn't throw the exception :-)

The likely culprit is the working directory differs from what is expected (that is, the current working directory does not contain a file with that name). This can be trivially verified with using the file's absolute path and observing that it is loaded correctly.

Alternatively, to find the current directory:

String cwd = new File(".").getAbsolutePath();

Happy coding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...