我有一个txt文件。它包含一个目录(H:/)。我想读这个目录。目录中还有一些csv文件。我想只看到csv文件。我认为,我的Java代码包含了所有相关内容。现在他找不到文本文件。文本文件位于Eclipse的项目文件夹中(所以我使用了相对路径) 我的错误在哪里?
编辑:我提出了一个我的问题的常见例子
public class AllFiles {
public static void main(String[]args) throws IOException
{
File dir = new File("C:/Users/Example/Main/Test.txt");
getAllFiles(dir);
} private static void getAllFiles(File dir) throws IOException {
// Read from the file
BufferedReader br = new BufferedReader(new FileReader(dir));
String path = br.readLine();
br.close();
File[] fileArray = new File (line).listFiles(new FilenameFilter() {
//only data with .csv were shown
public boolean accept(File dir, String name) {
return name.endsWith(".csv");
}
});
for(File f : fileArray){
if(f.isDirectory())
getAllFiles(f);
if(f.isFile()){
System.out.println(f.getName());
}
}
}
}
答案 0 :(得分:2)
您永远不会将文件内容分配给变量行。
将String line;
br.readLine();
更改为String line = br.readLine();
下一个错误是您尝试列出".../Users/example/Test.txt"
中的文件。你想尝试的是:
File[] fileArray = new File(line).listFiles(...