使用 FileReader 会导致编译器错误“未处理的异常类型 FileNotFoundException”

发布于 2024-11-17 01:30:01 字数 1047 浏览 1 评论 0原文

我在这里阅读了一些涉及相同问题的线程,但解决方案不起作用。 :/

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

国粹 2024-11-24 01:30:01

未处理的异常类型 FileNotFoundException myclass.java /myproject/src/mypackage

这是编译器错误。 Eclipse 告诉您您的程序未编译为 java 字节代码(因此您当然无法运行它)。现在,您可以通过简单地声明您的程序可能抛出此异常来修复它。就像这样:

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

FileNotFoundException 是一个“受检查的异常”(google this),这意味着代码必须声明 JVM 在遇到它时应该做什么。在代码中,try-catch 块或 throws 声明向 JVM 指示如何处理异常。

为了便于将来参考,请注意 Eclipse 中的红色波浪下划线表示存在编译器错误。如果将鼠标悬停在问题上,Eclipse 通常会建议一些非常好的解决方案。在这种情况下,一个建议是“向 main 添加一个 throws 子句”。

Unhandled exception type FileNotFoundException myclass.java /myproject/src/mypackage

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:

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

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".

终止放荡 2024-11-24 01:30:01

使用您在创建文件读取器之前创建并验证的文件描述符。此外,使用相对路径可能会遇到问题。为什么带有完整路径的行被注释掉?无论如何,代码如下:

if(file1.exists()) {
     log(myfile + " exists. length : " + myfile.length());
     FileReader fr = new FileReader(file1); 
}

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:

if(file1.exists()) {
     log(myfile + " exists. length : " + myfile.length());
     FileReader fr = new FileReader(file1); 
}
余厌 2024-11-24 01:30:01

我发现您尝试指定文件的完整路径,但由于以下错误而失败:

您尚未声明或尝试捕获 java.lang. io.FileNotFoundException

要修复此问题,您可以

FileReader fr = new FileReader("myscript.abc");

用以下代码替换该行:

try {
    FileReader fr = 
    new FileReader("/home/jason/workspace/myproject/src/mypackage/myscript.abc");
} catch (FileNotFoundException ex) {
    Logger.getLogger(myclass.class.getName()).log(Level.SEVERE, null, ex);
}

以下代码已成功编译,并且应该可以工作:

package mypackage;
import java.io.*;
// It's better to use Camel style name for class name, for example: MyClass.
// In such a way it'll be easier to distinguish class name from variable name.
// This is common practice in Java.
public class myclass {

    public static void main(String[] args) {
        String myfile = 
            "/home/jason/workspace/myproject/src/mypackage/myscript.abc";

        File file1 = new File(myfile);
        if (file1.exists()) {
            log("File " + myfile + " exists. length : " + myfile.length());
        } else {
            log("File " + myfile + " does not exist!");
        }

        try {
            FileReader fr = new FileReader(myfile);
        } catch (FileNotFoundException ex) {
            // Do something with mistake or ignore
            ex.printStackTrace();
        }

        log("\nAbsPath : " + new File(".").getAbsolutePath());
        log("\nuser.dir : " + System.getProperty("user.dir"));
    }

    public static void log(String s) {
        System.out.println(s);
    }
}

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

FileReader fr = new FileReader("myscript.abc");

with the code:

try {
    FileReader fr = 
    new FileReader("/home/jason/workspace/myproject/src/mypackage/myscript.abc");
} catch (FileNotFoundException ex) {
    Logger.getLogger(myclass.class.getName()).log(Level.SEVERE, null, ex);
}

The following code is successfully compiled, and it should work:

package mypackage;
import java.io.*;
// It's better to use Camel style name for class name, for example: MyClass.
// In such a way it'll be easier to distinguish class name from variable name.
// This is common practice in Java.
public class myclass {

    public static void main(String[] args) {
        String myfile = 
            "/home/jason/workspace/myproject/src/mypackage/myscript.abc";

        File file1 = new File(myfile);
        if (file1.exists()) {
            log("File " + myfile + " exists. length : " + myfile.length());
        } else {
            log("File " + myfile + " does not exist!");
        }

        try {
            FileReader fr = new FileReader(myfile);
        } catch (FileNotFoundException ex) {
            // Do something with mistake or ignore
            ex.printStackTrace();
        }

        log("\nAbsPath : " + new File(".").getAbsolutePath());
        log("\nuser.dir : " + System.getProperty("user.dir"));
    }

    public static void log(String s) {
        System.out.println(s);
    }
}
暮光沉寂 2024-11-24 01:30:01

这是一个编译器错误。 Eclipse 告诉您您的程序未编译为 java 字节代码(因此您当然无法运行它)。现在,您可以通过简单地声明您的程序可能抛出此异常来修复它。就像这样:

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

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:

public static void main(String[] args) throws IOException{
}
蓝戈者 2024-11-24 01:30:01

您期望 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).

眼泪也成诗 2024-11-24 01:30:01

与其试图弄清楚发生了什么,为什么不打印发生了什么...

对您的代码进行此更改:

log(myfile.getName() + "(full path=" + myfile.getAbsolutePath() + ") does not exist");

您可能会发现它要么没有使用您认为的目录,要么(取决于在你的文件系统上)它可能会尝试创建一个名称字面意思为“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:

log(myfile.getName() + "(full path=" + myfile.getAbsolutePath() + ") does not exist");

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.

榕城若虚 2024-11-24 01:30:01

您可以简单地声明抛出此异常来修复它。像这样:

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


        FileReader reader=new FileReader("db.properties");

        Properties p=new Properties();
        p.load(reader);
    }

you can fix it simply declaring that throw this exception. Like this:

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


        FileReader reader=new FileReader("db.properties");

        Properties p=new Properties();
        p.load(reader);
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文