使用 FileReader 会导致编译器错误“未处理的异常类型 FileNotFoundException”
我在这里阅读了一些涉及相同问题的线程,但解决方案不起作用。 :/
我使用 Eclipse,这是我的程序。
package mypackage;
import java.io.*;
public class myclass {
public static void main(String[] args) {
//String myfile = "/home/jason/workspace/myproject/src/mypackage/myscript.abc";
String myfile = "src/mypackage/myscript.abc";
File file1 = new File(myfile);
if(file1.exists()) {
log(myfile + " exists. length : " + myfile.length());
}
else{
log(myfile + " does not exist");
//System.exit(1);
}
//FileReader fr = new FileReader("myscript.abc");//I uncomment this and die inside
System.out.println("\nAbsPath : " + new File(".").getAbsolutePath());
System.out.println("\nuser.dir : " + System.getProperty("user.dir"));
}
public static void log(String s){
System.out.println(s);
}
:
无论我尝试什么,或者将 myscript.abc (现在遍布整个项目目录)放在哪里,我得到的错误都是这样的
未处理的异常类型 FileNotFoundException myclass.java /myproject/src/mypackage
束手无策,揪头发。
Ive read a few threads here that relate the same problem, but the solutions arent working. :/
I use Eclipse, here is my program.
package mypackage;
import java.io.*;
public class myclass {
public static void main(String[] args) {
//String myfile = "/home/jason/workspace/myproject/src/mypackage/myscript.abc";
String myfile = "src/mypackage/myscript.abc";
File file1 = new File(myfile);
if(file1.exists()) {
log(myfile + " exists. length : " + myfile.length());
}
else{
log(myfile + " does not exist");
//System.exit(1);
}
//FileReader fr = new FileReader("myscript.abc");//I uncomment this and die inside
System.out.println("\nAbsPath : " + new File(".").getAbsolutePath());
System.out.println("\nuser.dir : " + System.getProperty("user.dir"));
}
public static void log(String s){
System.out.println(s);
}
}
The error I get, no matter what I try, or where I put myscript.abc (its peppered throughout the projects directory now) is this :
Unhandled exception type
FileNotFoundException myclass.java /myproject/src/mypackage
Wits end, pulling hairs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是编译器错误。 Eclipse 告诉您您的程序未编译为 java 字节代码(因此您当然无法运行它)。现在,您可以通过简单地声明您的程序可能抛出此异常来修复它。就像这样:
FileNotFoundException
是一个“受检查的异常”(google this),这意味着代码必须声明 JVM 在遇到它时应该做什么。在代码中,try-catch 块或 throws 声明向 JVM 指示如何处理异常。为了便于将来参考,请注意 Eclipse 中的红色波浪下划线表示存在编译器错误。如果将鼠标悬停在问题上,Eclipse 通常会建议一些非常好的解决方案。在这种情况下,一个建议是“向 main 添加一个 throws 子句”。
This is a compiler error. Eclipse is telling you that your program does not compile to java byte code (so of course you can't run it). For now, you can fix it by simply declaring that your program may throw this exception. Like so:
FileNotFoundException
is a "checked exception" (google this) which means that the code has to state what the JVM should do if it is encountered. In code, a try-catch block or a throws declaration indicate to the JVM how to handle the exception.For future reference, please note that the red squiggly underline in Eclipse means there is a compiler error. If you hover the mouse over the problem, Eclipse will usually suggest some very good solutions. In this case, one suggestion would be to "add a throws clause to main".
使用您在创建文件读取器之前创建并验证的文件描述符。此外,使用相对路径可能会遇到问题。为什么带有完整路径的行被注释掉?无论如何,代码如下:
Use the file descriptor that you created and verified before creating the file reader. Also, you will probably run into problems using relative paths. Why is the line with the full path commented out? In any case, here is the code:
我发现您尝试指定文件的完整路径,但由于以下错误而失败:
您尚未声明或尝试捕获
java.lang. io.FileNotFoundException
。要修复此问题,您可以
用以下代码替换该行:
以下代码已成功编译,并且应该可以工作:
I see that you tried to specify the full path to your file, but failed because of the following mistake:
you haven't declared or tried to catch
java.io.FileNotFoundException
.To fix it, you can replace the line
with the code:
The following code is successfully compiled, and it should work:
这是一个编译器错误。 Eclipse 告诉您您的程序未编译为 java 字节代码(因此您当然无法运行它)。现在,您可以通过简单地声明您的程序可能抛出此异常来修复它。就像这样:
This is a compiler error. Eclipse is telling you that your program does not compile to java byte code (so of course you can't run it). For now, you can fix it by simply declaring that your program may throw this exception. Like so:
您期望 Eclipse 运行项目根目录中的程序。除非您对“运行”配置做了一些特殊的操作,否则如果它真的从那里开始,我会感到惊讶。
尝试打印出当前的工作目录以确保这是正确的路径。
然后尝试验证 bin / build 目录是否包含您的“*.abc”文件,因为它们不是 Java 源文件,并且可能尚未复制到编译输出目录中。
假设它们位于 compliation 目录中,请重写文件加载器以使用基于类 laoder 路径的相对路径。这对于目录中 .class 文件的扩展集合(以及后来在打包的 JAR 文件中)非常有效。
You are expecting Eclipse to run the program in the project root directory. Unless you did something special with your "Run" configuration, I'd be suprised if it really starts there.
Try printing out your current working directory to make sure this is the right path.
Then try verifying that the bin / build directory contains your "*.abc" files, as they are not Java source files and may have not been copied into the compilation output directory.
Assuming that they are in the compliation directory, rewrite your file loader to use a relative path based on the class laoder's path. This will work well in exanded collections of .class files in directories (and later in packed JAR files).
与其试图弄清楚发生了什么,为什么不打印发生了什么...
对您的代码进行此更改:
您可能会发现它要么没有使用您认为的目录,要么(取决于在你的文件系统上)它可能会尝试创建一个名称字面意思为“src/mypackage/myscript.abc”的文件 - 即带有嵌入斜杠的文件名。
Instead of trying to figure out what's going on, why not print what's going on...
Make this change to your code:
You might find it either isn't using the directory you think, or (depending on your filesystem) it might be trying to create a file whose name is literally
"src/mypackage/myscript.abc"
- ie a filename with embedded slashes.您可以简单地声明抛出此异常来修复它。像这样:
you can fix it simply declaring that throw this exception. Like this: