0
votes

I put two files in a directory and tested to see if my code can search through the files and find a match, but the FileReader won't read the second file. Here is my code and my console entry. I have narrowed the error down to the FileReader, but I don't know how to fix that.

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);
    }
  }
}

Here is the output of my console:

Enter the search term: 
bla
Enter each file location: 
test dir
Found Files:
testfile.txt
testfile2.txt
Unable to open file 'test dir'

The entire stack trace of the error is:

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)

Please help. Thank you.

2
Try instead of printing a message in the catch block, print the entire stack trace of the error. (with ex.printStackTrace()). It could give you more info on why the program can't find the file to read.MatheM
Secondly you are already iterating over array of file names with this for (String file : files) so when creating your FileReader you don't have to use index to get the filename from the array. So instead of fileReader = new FileReader(files[f1]); you can do fileReader = new FileReader(file);MatheM
Thanks for that code-cleanup advice. That didn't fix it but that helped make things more simplistic.B. Brewer
Do you know how to read information stack trace is giving you? This line (com.mangodev.Main.searchEngine(Main.java:32)) imeans that error is on line 32 I am assuming that it is the fileReader = new FileReader(files[f1]); line. And the exception FileNotFoundException means that the computer is not able to find the second file or it could mean that some other program has opened that file for exclusive access. Are you sure that the second file is not opened or locked by another program?MatheM
I knew it came from the FileReader and I knew what the FileNotFoundException was but I didn't know how to fix it or if a program had exclusive access. Maybee it is because I have it in my Eclipse folder?B. Brewer

2 Answers

0
votes

It looks to me as if you have the following folder structure:

Main.class
Main.java
test dir
|-- testfile.txt
|-- testfile2.txt

You run the code from the directory containing Main.class, Main.java and test dir. Your code then lists files in the directory test dir, finding the two text files it contains, but then attempts to open them from the current directory. This is the parent directory, and of course, this isn't where those files are. They are in the sub-directory test dir. A FileNotFoundException is therefore to be expected: you're attempting to open a file in the wrong directory.

If the FileReader happens to fail on the second of the two files, does there happen to be a file testfile.txt in the parent directory as well? Your code may well have been opening this file first time through the loop instead of the one in test dir that you thought it was.

To open files within the test dir subdirectory, replace the line

        fileReader = new FileReader(files[f1]);

with

        fileReader = new FileReader(new File(dir, files[f1]));
0
votes

In your first line in the searchEngine method you create a variable folder that contains the files in the directory. I suggest using this variable directly in your for loop instead of string filenames.

for (File file : folder.listFiles()) {
    linematches = new ArrayList();
    fileReader = new FileReader(file);
    bufferedReader = new BufferedReader(fileReader);
    //rest of code...
}