FileNotFoundException when creating a Scanner in Eclipse with Java

♀尐吖头ヾ 提交于 2021-02-05 05:43:46

问题


I'm getting a FileNotFoundException when running the following Java 6 code on Eclipse (Indigo) on Snow Leopard:

import java.io.*;
import java.util.*;

public class readFile {

    public static void main(String[] args) {

        Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt"));  // Line 9

    }
}

The exception is

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Unhandled exception type FileNotFoundException

    at readFile.main(readFile.java:9)

My current workspace is /Users/daniel/pr/java. It contains only one project (readFile), and the file hierarchy looks like this:

- readFile
    - src
        - (default package)
            - readFile.java
    - JRE System Library [JavaSE-1.6]
    - myfile.txt

After reading several very similar questions, I've tried

  • placing copies of myfile.txt in the project, bin, src, and workspace directories, as well as my home and root folders
  • identifying the working directory and using a relative path
  • manually setting the workspace via "Run Configurations > Arguments > Working Directory" in Eclipse
  • running the program with the command line Java launcher in the bin, readFile, src, and java directories (with copies of myfile.txt in all of these places)
  • removing the file extension and/or lengthening the filename (above some supposed minimum character limit), and
  • verifying the permissions of myfile.txt (they're now rw-r--r--).

I'm at a loss. What could be the problem? (Thank you for reading!)


回答1:


The exception tells you the problem.

The code you have in your main might throw a FileNotFoundException, so you need to consider that in your code, either by declaring in the method signature that that exception can be thrown, or by surrounding the code with a try catch:

Declaring:

public static void main(String[] args) throws FileNotFoundException{

    Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt"));  // Line 9

}

Or using try/catch

public static void main(String[] args) {
    try { 
        Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt"));  // Line 9
    } catch (FileNotFoundException e) {
        //do something with e, or handle this case
    }
}

The difference between these two approaches is that, since this is your main, if you declare it in the method signature, your program will throw the Exception and stop, giving you the stack trace.

If you use try/catch, you can handle this situation, either by logging the error, trying again, etc.

You might want to give a look at: http://docs.oracle.com/javase/tutorial/essential/exceptions/ to learn about exception handling in Java, it'll be quite useful.




回答2:


FileNotFoundException is a checked exception ! You must catch the exception ...

 public static void main(String[] args) {
     try {
        Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt"));  // Line 9
     } catch(FileNotFoundException ex) {
         //Handle exception code ...
     }
 }



回答3:


"/Users/daniel/pr/java/readFile/myfile.txt"

Shouldn't that be:

"/users/daniel/pr/java/readFile/myfile.txt"


来源:https://stackoverflow.com/questions/10452948/filenotfoundexception-when-creating-a-scanner-in-eclipse-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!