public class Cww {
static List<String> readFile(String filename) {
List<String> records = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while((line = reader.readLine()) != null)
{
records.add(line);
}
reader.close();
return records;
} catch(Exception e) {
System.out.println(e);
return null;
}
}
和我的主要:
readFile("DirList.java");
File file = new File("DirList.java");
System.out.println(file.getCanonicalPath());
// CLASSPATH: .;..;J:\Programowanie\eclipse workspace\tij;C:\Program Files\Java\jre7\lib\ext\QTJava.zip
output: java.io.FileNotFoundException: DirList.java (Nie można odnaleźć określonego pliku)
J:\Programowanie\eclipse workspace\Rozdzial 18 cwiczenia\DirList.java
file.getCanonicalPath()
显示 jvm 搜索我的文件的实际位置,但我的 fileRead
方法仍然给我错误,
我是否需要将每个项目文件夹包含在类路径中才能从中读取文件?
提前致谢
最佳答案
File
构造函数参数是绝对或相对文件名。它不会使用类路径,文件名 - 如果不是绝对的 - 始终相对于当前工作目录。
关于java - 我的方法无法在目录中找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9290573/