eclipse中的java.io.FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22978170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.FileNotFoundException in eclipse
提问by user2948723
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 的人能帮我解决这个问题,我将不胜感激!
采纳答案by Justin Jasmann
You've used a relativefile 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 directoryof your project. Like so:
如果您想这样做,只需将该strength.txt
文件放在项目的基本目录中即可。像这样:
Alternatively, you could reference the absolute
file path on your system. For example, use:
或者,您可以引用absolute
系统上的文件路径。例如,使用:
Windows:
视窗:
C:/dev/myproject/strength.txt
C:/dev/myproject/strength.txt
Mac/Unix:
Mac/Unix:
/Users/username/dev/strength.txt
/Users/username/dev/strength.txt
(or whatever the full path may be) instead.
(或任何可能的完整路径)。
回答by spiderman
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 期望在哪里找到文件,以及它是否也是你期望的文件夹,相应地放置文件或给出确切的位置
回答by Justin Jasmann
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)
当我在路径中使用它时,我发现这为我解决了这个问题(这看起来很奇怪,因为它应该是它所在的位置,但它有效)
回答by Franklin Yu
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:
如果这对您有用,您可以通过以下方式将执行目录从项目根目录更改为二进制目录:
- "Run" -> "Run configurations"
- In the "Arguments" tab, find "working directory" settings.
- Switch from "Default" to "Other".
- 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)
. - Append "/bin" to the end of it; save the configuration.
- “运行”->“运行配置”
- 在“参数”选项卡中,找到“工作目录”设置。
- 从“默认”切换到“其他”。
- 单击“工作区”按钮,在弹出的窗口中选择项目,然后单击“确定”;这会给你带来类似的东西
$(workspace_loc:proj-1)
。 - 将“/bin”附加到它的末尾;保存配置。
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.
我需要这个而不是在我做作业时简单地将文件放在项目根目录中,而教授需要特定的文件层次结构;此外,这比绝对路径更便携。
回答by Franklin Yu
Inside the base directory create a folder, name it "res". Place your file inside "res" folder.
在基本目录中创建一个文件夹,将其命名为“res”。将您的文件放在“res”文件夹中。
use String file = ".\\res\\strength.txt";
to reference the location of your file.
用于String file = ".\\res\\strength.txt";
引用文件的位置。
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.
并从您的根目录引用该文件。因此该文件存在于包中。