使用 Java 在 Eclipse 中创建扫描仪时出现 FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10452948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileNotFoundException when creating a Scanner in Eclipse with Java
提问by Daniel Jordan
I'm getting a FileNotFoundException when running the following Java 6 code on Eclipse (Indigo) on Snow Leopard:
在 Snow Leopard 上的 Eclipse (Indigo) 上运行以下 Java 6 代码时,我收到 FileNotFoundException:
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:
我当前的工作区是 /Users/daniel/pr/java。它只包含一个项目(readFile),文件层次结构是这样的:
- 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--).
- 将 myfile.txt 的副本放置在项目、bin、src 和工作区目录以及我的主文件夹和根文件夹中
- 识别工作目录并使用相对路径
- 通过 Eclipse 中的“运行配置 > 参数 > 工作目录”手动设置工作区
- 在 bin、readFile、src 和 java 目录中使用命令行 Java 启动器运行程序(在所有这些位置都有 myfile.txt 的副本)
- 删除文件扩展名和/或延长文件名(超过一些假设的最小字符限制),以及
- 验证 myfile.txt 的权限(它们现在是 rw-r--r--)。
I'm at a loss. What could be the problem? (Thank you for reading!)
我不知所措。可能是什么问题呢?(感谢阅读!)
回答by pcalcao
The exception tells you the problem.
异常告诉你问题所在。
The code you have in your main mightthrow 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:
您在 main 中的代码可能会抛出 FileNotFoundException,因此您需要在代码中考虑这一点,方法是在方法签名中声明可以抛出该异常,或者使用 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
或者使用 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.
如果您使用 try/catch,您可以通过记录错误、重试等来处理这种情况。
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.
您可能想看看:http: //docs.oracle.com/javase/tutorial/essential/exceptions/以了解 Java 中的异常处理,它会非常有用。
回答by aleroot
FileNotFoundExceptionis a checked exception ! You must catch the exception ...
FileNotFoundException是一个检查异常!您必须捕获异常...
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 ...
}
}
回答by Andrew Thompson
"/Users/daniel/pr/java/readFile/myfile.txt"
Shouldn't that be:
不应该是:
"/users/daniel/pr/java/readFile/myfile.txt"