简体   繁体   English

eclipse中的java.io.FileNotFoundException

[英]java.io.FileNotFoundException in eclipse

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:这是我为类编写的程序的一小部分(我知道尚未使用两个私有属性)但是当我尝试使用 strength.txt 文件运行它时,我收到以下错误:

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!如果有 Eclipse 的人能帮我解决这个问题,我将不胜感激!

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.如果您想这样做,只需将strength.txt文件放在项目的基本目录中。 Like so:像这样:

在此处输入图片说明

Alternatively, you could reference the absolute file path on your system.或者,您可以引用系统上的absolute文件路径。 For example, use:例如,使用:

Windows:视窗:

C:/dev/myproject/strength.txt

Mac/Unix: Mac/Unix:

/Users/username/dev/strength.txt

(or whatever the full path may be) instead. (或任何可能的完整路径)。

Do this做这个

System.out.println(openFile.getAbsolutePath());

It will show you where JVM expects to find the file and whether it is the folder you expect as well, Accordingly place the file or give the exact location它会告诉你 JVM 期望在哪里找到文件,以及它是否也是你期望的文件夹,相应地放置文件或给出确切的位置

Use this to see what file path the system is using to reach the relative path使用它来查看系统用于到达相对路径的文件路径

    System.out.print(System.getProperty("user.dir"));

Then make sure that the relative path immediately follows this path.然后确保相对路径紧跟此路径。

You can also turn that into a string by doing你也可以把它变成一个字符串

String filePath = System.getProperty("user.dir");

and then you can just add that to the beginning of the filepath like so,然后你可以像这样将它添加到文件路径的开头,

    ImageIcon imageIconRefVar = new ImageIcon(filePath + "/imagepathname");

I found this solved the issue for me when I used it in the path (which seemed odd since that should be the location it is in, but it worked)当我在路径中使用它时,我发现这为我解决了这个问题(这看起来很奇怪,因为它应该是它所在的位置,但它有效)

You've used a relative file path which is relative to your project execution.您使用了相对于您的项目执行的相对文件路径。

If this works for you, you can change the execution directory from the project root to the binary directory by:如果这对您有用,您可以通过以下方式将执行目录从项目根目录更改为二进制目录:

  1. "Run" -> "Run configurations" “运行”->“运行配置”
  2. In the "Arguments" tab, find "working directory" settings.在“参数”选项卡中,找到“工作目录”设置。
  3. Switch from "Default" to "Other".从“默认”切换到“其他”。
  4. Click the "Workspace" button and select the project in pop-up window then click "OK";单击“工作区”按钮,在弹出的窗口中选择项目,然后单击“确定”; this will bring you something like $(workspace_loc:proj-1) .这会给你带来类似$(workspace_loc:proj-1)
  5. Append "/bin" to the end of it;将“/bin”附加到它的末尾; save the configuration.保存配置。

I need this instead of simply putting files in project root directory when I am doing assignment and the professor requires specific file hierarchy;我需要这个而不是在我做作业时简单地将文件放在项目根目录中,而教授需要特定的文件层次结构; in addition, this is more portable than absolute path.此外,这比绝对路径更便携。

Inside the base directory create a folder, name it "res".在基本目录中创建一个文件夹,将其命名为“res”。 Place your file inside "res" folder.将您的文件放在“res”文件夹中。

use String file = ".\\\\res\\\\strength.txt";使用String file = ".\\\\res\\\\strength.txt"; to reference the location of your file.以引用文件的位置。

You should use a resource folder to store all the files you use in your program (a good practice).您应该使用资源文件夹来存储您在程序中使用的所有文件(一种很好的做法)。

And make a refrence of that file from your root directory.并从您的根目录引用该文件。 So the file is present within the package.所以该文件存在于包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM