FileReader 不会读取列表中的第二个文件
FileReader won't read second file in a list
我将两个文件放在一个目录中并测试我的代码是否可以搜索文件并找到匹配项,但是 FileReader
不会读取第二个文件。这是我的代码和我的控制台条目。我已将错误缩小到 FileReader
,但我不知道如何解决。
public class Main
{
public static void searchEngine(String dir, String Search)
{
File folder = new File(dir);
String[] files = folder.list();
Integer f1 = 0;
FileReader fileReader;
ArrayList linematches;
BufferedReader bufferedReader;
Integer q;
String line;
Integer linenum;
System.out.println("Found Files:");
for (String file : files) {
System.out.println(file);
}
try {
for (String file : files) {
linematches = new ArrayList();
fileReader = new FileReader(files[f1]);
bufferedReader = new BufferedReader(fileReader);
linenum = 0;
while ((line = bufferedReader.readLine()) != null) {
linenum += 1;
if (line.contains(Search)) {
linematches.add(linenum);
}
}
q = 0;
for (int i = 0; i < linematches.size(); i++) {
System.out.println("File: " + file + " Line: " + linematches.get(i));
}
linematches.removeAll(linematches);
// Always close files.
bufferedReader.close();
f1++;
}
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + dir + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + dir + "'");
}
}
public static void main(String[] args)
{
while (true) {
System.out.println("Enter the search term: ");
Scanner scanner = new Scanner(System.in);
String searchterm = scanner.nextLine();
System.out.println("Enter each file location: ");
String f1 = scanner.nextLine();
searchEngine(f1, searchterm);
}
}
}
这是我的控制台的输出:
Enter the search term:
bla
Enter each file location:
test dir
Found Files:
testfile.txt
testfile2.txt
Unable to open file 'test dir'
错误的整个堆栈跟踪是:
Unable to open file 'testfile2.txt' java.io.FileNotFoundException:
testfile2.txt (No such file or directory) Enter the search term: at
java.io.FileInputStream.open0(Native Method) at
java.io.FileInputStream.open(FileInputStream.java:195) at
java.io.FileInputStream.(FileInputStream.java:138) at
java.io.FileInputStream.(FileInputStream.java:93) at
java.io.FileReader.(FileReader.java:58) at
com.mangodev.Main.searchEngine(Main.java:32) at
com.mangodev.Main.main(Main.java:70)
请帮忙。谢谢。
在我看来,您的文件夹结构如下:
Main.class
Main.java
test dir
|-- testfile.txt
|-- testfile2.txt
您 运行 包含 Main.class
、Main.java
和 test dir
的目录中的代码。然后您的代码会列出目录 test dir
中的文件,找到其中包含的两个文本文件,然后尝试从当前目录 打开它们 。这是父目录,当然,这不是那些文件所在的位置。它们位于子目录 test dir
中。因此 FileNotFoundException
是意料之中的:您试图在错误的目录中打开文件。
如果 FileReader 恰好在两个文件中的第二个文件上失败,那么父目录中是否也碰巧有一个文件 testfile.txt
?您的代码很可能是在循环中第一次打开此文件,而不是您认为的 test dir
中的文件。
要打开 test dir
子目录中的文件,请替换行
fileReader = new FileReader(files[f1]);
和
fileReader = new FileReader(new File(dir, files[f1]));
在 searchEngine
方法的第一行中,您创建了一个包含目录中文件的变量 folder
。我建议直接在你的 for 循环中使用这个变量而不是字符串文件名。
for (File file : folder.listFiles()) {
linematches = new ArrayList();
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
//rest of code...
}
我将两个文件放在一个目录中并测试我的代码是否可以搜索文件并找到匹配项,但是 FileReader
不会读取第二个文件。这是我的代码和我的控制台条目。我已将错误缩小到 FileReader
,但我不知道如何解决。
public class Main
{
public static void searchEngine(String dir, String Search)
{
File folder = new File(dir);
String[] files = folder.list();
Integer f1 = 0;
FileReader fileReader;
ArrayList linematches;
BufferedReader bufferedReader;
Integer q;
String line;
Integer linenum;
System.out.println("Found Files:");
for (String file : files) {
System.out.println(file);
}
try {
for (String file : files) {
linematches = new ArrayList();
fileReader = new FileReader(files[f1]);
bufferedReader = new BufferedReader(fileReader);
linenum = 0;
while ((line = bufferedReader.readLine()) != null) {
linenum += 1;
if (line.contains(Search)) {
linematches.add(linenum);
}
}
q = 0;
for (int i = 0; i < linematches.size(); i++) {
System.out.println("File: " + file + " Line: " + linematches.get(i));
}
linematches.removeAll(linematches);
// Always close files.
bufferedReader.close();
f1++;
}
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + dir + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + dir + "'");
}
}
public static void main(String[] args)
{
while (true) {
System.out.println("Enter the search term: ");
Scanner scanner = new Scanner(System.in);
String searchterm = scanner.nextLine();
System.out.println("Enter each file location: ");
String f1 = scanner.nextLine();
searchEngine(f1, searchterm);
}
}
}
这是我的控制台的输出:
Enter the search term:
bla
Enter each file location:
test dir
Found Files:
testfile.txt
testfile2.txt
Unable to open file 'test dir'
错误的整个堆栈跟踪是:
Unable to open file 'testfile2.txt' java.io.FileNotFoundException:
testfile2.txt (No such file or directory) Enter the search term: at
java.io.FileInputStream.open0(Native Method) at
java.io.FileInputStream.open(FileInputStream.java:195) at
java.io.FileInputStream.(FileInputStream.java:138) at
java.io.FileInputStream.(FileInputStream.java:93) at
java.io.FileReader.(FileReader.java:58) at
com.mangodev.Main.searchEngine(Main.java:32) at
com.mangodev.Main.main(Main.java:70)
请帮忙。谢谢。
在我看来,您的文件夹结构如下:
Main.class
Main.java
test dir
|-- testfile.txt
|-- testfile2.txt
您 运行 包含 Main.class
、Main.java
和 test dir
的目录中的代码。然后您的代码会列出目录 test dir
中的文件,找到其中包含的两个文本文件,然后尝试从当前目录 打开它们 。这是父目录,当然,这不是那些文件所在的位置。它们位于子目录 test dir
中。因此 FileNotFoundException
是意料之中的:您试图在错误的目录中打开文件。
如果 FileReader 恰好在两个文件中的第二个文件上失败,那么父目录中是否也碰巧有一个文件 testfile.txt
?您的代码很可能是在循环中第一次打开此文件,而不是您认为的 test dir
中的文件。
要打开 test dir
子目录中的文件,请替换行
fileReader = new FileReader(files[f1]);
和
fileReader = new FileReader(new File(dir, files[f1]));
在 searchEngine
方法的第一行中,您创建了一个包含目录中文件的变量 folder
。我建议直接在你的 for 循环中使用这个变量而不是字符串文件名。
for (File file : folder.listFiles()) {
linematches = new ArrayList();
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
//rest of code...
}