eclipse中的java.io.FileNotFoundException异常 [英] java.io.FileNotFoundException in eclipse

查看:1291
本文介绍了eclipse中的java.io.FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:



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

public class Driver {

private int colorStrength;
私人字符串颜色;

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

String line,file =strength.txt;

文件openFile =新文件(文件);
扫描仪inFile = new Scanner(openFile);

while(inFile.hasNext()){
line = inFile.nextLine();
System.out.println(line);
}

inFile.close();
}
}

这是我正在写的一个程序的一小部分对于一个类(两个私有属性还没有被使用我知道),但是当我尝试用strength.txt文件运行它时,我收到以下错误:



例外:



 线程main中的异常java.io.FileNotFoundException:strength.txt(系统找不到指定的文件) 
在java.io.FileInputStream.open(本机方法)
在java.io.FileInputStream。< init>(未知源)
在java.util.Scanner。< init> (未知来源)
在Driver.main(Driver.java:14)

如果有人与Eclipse可以帮助我弄清楚,这将是非常感谢!

解决方案

您已经使用了相对于项目执行的相对相对文件路径。 p>

如果你想这样做,只需将 strength.txt 文件放在基础中目录。像这样:





或者,您可以参考系统上的绝对文件路径。例如,使用:



Windows:



C:/ dev / myproject / strong.txt



Mac / Unix:



/Users/username/dev/strength.txt



(或任何完整路径可能是)。


Code:

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

public class Driver {

    private int colorStrength;
    private String color;

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

        String line, file = "strength.txt";

        File openFile = new File(file);
        Scanner inFile = new Scanner(openFile);

        while (inFile.hasNext()) {
            line = inFile.nextLine();
            System.out.println(line);
        }

        inFile.close();
    }
}

This is a small part of a program I am writing for a class (the two private attributes have yet to be used I know) but when I try to run this with the strength.txt file I receive the following errors:

Exception:

Exception in thread "main" java.io.FileNotFoundException: strength.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Driver.main(Driver.java:14)

If anyone with Eclipse could help me figure this out it would be much appreciated!

解决方案

You've used a relative file path which is relative to your project execution.

If you'd like to do it that way, simply put the strength.txt file in the base directory of your project. Like so:

Alternatively, you could reference the absolute file path on your system. For example, use:

Windows:

C:/dev/myproject/strength.txt

Mac/Unix:

/Users/username/dev/strength.txt

(or whatever the full path may be) instead.

这篇关于eclipse中的java.io.FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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