Java Eclipse - 系统找不到指定的文件 (java.io.FileNotFoundException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29335460/
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 Eclipse - The system cannot find the file specified ( java.io.FileNotFoundException )
提问by user2041390
In my code, I would like to read input from a file, and I am getting an exception.
在我的代码中,我想从文件中读取输入,但出现异常。
Exception in thread "main" java.io.FileNotFoundException: TestValues.csv (The system cannot find the file specified)
线程“main”中的异常 java.io.FileNotFoundException: TestValues.csv(系统找不到指定的文件)
Exception in thread "main" java.io.FileNotFoundException: (The system cannot find the file specified)
线程“main”中的异常 java.io.FileNotFoundException:(系统找不到指定的文件)
Project Hierarchy in Eclipse :
Eclipse 中的项目层次结构:
Project folder
src
Package Folder
FileName.java
JRE System Library
Data
TestValues.txt
How to solve this problem ?
如何解决这个问题呢 ?
How to find file path, in java while using eclipse?
使用eclipse时如何在java中查找文件路径?
How to find relative file path, instead of using long absolute path name ?
如何查找相对文件路径,而不是使用长绝对路径名?
回答by user2041390
After seeing the question several times, and still struggling, I wanted to share the approach I followed to solve the problem.
看了好几遍问题,还在纠结,想分享一下我解决问题的方法。
package javacertification;
import java.io.File;
import java.io.IOException;
public class FindFileName {
public static void main(String[] args) throws IOException {
FindFileName ffn = new FindFileName();
ffn.printFilePath();
}
public void printFilePath(){
// http://stackoverflow.com/questions/681059/read-from-file-in-eclipse
System.out.println("1. Throws Exception as file does not Exists, Dummy File Name, to find the path");
File file = new File("TestValues.txt");
System.out.println("\t" + "Path : " + file.getAbsolutePath());
System.out.println("");
System.out.println("2. File will be read, as file Exists, under the Data folder, and path is correct");
file = new File("Data\TestValues.txt");
System.out.println("\t"+ "Path : " + file.getAbsolutePath());
}
}
Output:
输出:
Throws Exception as file does not Exists, Dummy File Name, to find the path Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\TestValues.txt
File will be read, as file Exists, under the Data folder, and path is correct Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\Data\TestValues.txt
抛出 Exception as file does not Exists, Dummy File Name, to find the path Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\TestValues.txt
文件将被读取,作为文件 Exists,在 Data 文件夹下,并且路径是正确的 Path : C:\WorkSpace\CodeWorkSpace\OCJD\JavaCertification\Data\TestValues.txt