使用Java在Eclipse中创建扫描仪时出现FileNotFoundException [英] FileNotFoundException when creating a Scanner in Eclipse with Java

查看:73
本文介绍了使用Java在Eclipse中创建扫描仪时出现FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Snow Leopard的Eclipse(Indigo)上运行以下Java 6代码时,出现FileNotFoundException:

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

    }
}

例外是

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

    at readFile.main(readFile.java:9)

我当前的工作空间是/Users/daniel/pr/java.它仅包含一个项目(readFile),并且文件层次结构如下所示:

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

  • 将myfile.txt的副本放置在项目,bin,src和工作区目录以及我的主文件夹和根文件夹中
  • 标识工作目录并使用相对路径
  • 通过Eclipse中的运行配置>参数>工作目录"手动设置工作区
  • 在bin,readFile,src和java目录(在所有这些位置都有myfile.txt的副本)中,使用命令行Java启动器运行程序
  • 删除文件扩展名和/或延长文件名(超出某些假定的最小字符限制),并且
  • 验证myfile.txt(现在为rw-r--r--)的权限.

我很茫然.可能是什么问题呢? (谢谢您的阅读!)

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

推荐答案

异常告诉您问题所在.

您的主可能中的代码会引发FileNotFoundException,因此您需要在代码中考虑这一点,方法是在方法签名中声明可以引发该异常,或者将其包围尝试捕获的代码:

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:

声明:

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

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

}

或使用try/catch

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
    }
}

这两种方法之间的区别在于,由于这是您的主要对象,因此,如果您在方法签名中声明它,则程序将抛出Exception并停止,从而为您提供堆栈跟踪.

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.

如果使用try/catch,则可以通过记录错误,重试等等来处理这种情况.

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

您可能想看看: http://docs.oracle.com/javase/tutorial/essential/exceptions/了解Java中的异常处理,这将非常有用.

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.

这篇关于使用Java在Eclipse中创建扫描仪时出现FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆