在同一文件夹 Java 中找不到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28687148/
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
File not found in same folder Java
提问by Jeremy Fisher
I am trying to read a file in Java. I wrote a program and saved the file in the exact same folder as my program. Yet, I keep getting a FileNotFoundException. Here is the code:
我正在尝试用 Java 读取文件。我编写了一个程序并将文件保存在与我的程序完全相同的文件夹中。然而,我不断收到 FileNotFoundException。这是代码:
public static void main(String[] args) throws IOException {
Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
File f = new File("file.txt");
ArrayList<String> al = readFile(f, ht);
}
public static ArrayList<String> readFile(File f, Hashtable<String, Integer> ht) throws IOException{
ArrayList<String> al = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(f));
String line = "";
int ctr = 0;
}
...
return al;
}
Here is the stack trace:
这是堆栈跟踪:
Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at csfhomework3.ScramAssembler.readFile(ScramAssembler.java:26)
at csfhomework3.ScramAssembler.main(ScramAssembler.java:17)
I don't understand how the file can't be found if it's in the exact same folder as the program. I'm running the program in eclipse and I checked my run configurations for any stray arguments and there are none. Does anyone see what's wrong?
如果文件与程序位于完全相同的文件夹中,我不明白如何找不到该文件。我在 eclipse 中运行程序,我检查了我的运行配置是否有任何杂散参数,但没有。有没有人看到有什么问题?
回答by Elliott Frisch
Because the File
isn't where you think it is. Print the path that your program is attempting to read.
因为这File
不是你认为的地方。打印您的程序试图读取的路径。
File f = new File("file.txt");
try {
System.out.println(f.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
Per the File.getCanonicalPath()
javadoc, A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent.
根据File.getCanonicalPath()
javadoc,规范路径名既是绝对的又是唯一的。规范形式的精确定义取决于系统。
回答by chrisinmtown
Check the Eclipse run configuration. Look on the second tab "Arguments", at the bottom pane where it says "Working Directory". That's the place where the program gets launched and where it will expect to find the file. Usually in Eclipse it launches your program with the current working directory set to be the base project directory. If you have the java class file in a source folder and are using proper package (e.g., "com.mycompany.package"), then the data file will be in a directory like "src/com/mycompany/package" which is quite a different directory from the project directory.
检查 Eclipse 运行配置。查看第二个选项卡“参数”,在底部窗格中显示“工作目录”。这是程序启动的地方,也是它希望找到文件的地方。通常在 Eclipse 中,它会启动您的程序,并将当前工作目录设置为基本项目目录。如果您在源文件夹中有 java 类文件并且正在使用正确的包(例如,“com.mycompany.package”),那么数据文件将位于“src/com/mycompany/package”之类的目录中与项目目录不同的目录。
HTH
HTH
回答by delephin
You should probably check out this question: Java can't find file when running through Eclipse
你可能应该看看这个问题:Java can't find file when running through Eclipse
Basically you need to be sure where the execution is taking place (current directory) and how your SO will resolve the relative paths. Try changing the working directory in Eclipse' Run Configurations>Argumentsor provide the absolute filename
基本上,您需要确定执行发生的位置(当前目录)以及您的 SO 将如何解析相对路径。尝试在 Eclipse 的“运行配置”>“参数”中更改工作目录或提供绝对文件名
回答by Juned Ahsan
File needs to be in the class path and not in the source path. Copy the file in output/class files folder and it should be available.
文件需要在类路径中,而不是在源路径中。将文件复制到 output/class files 文件夹中,它应该可用。