File search for user entered phrase

132 views Asked by At

Hello. I am trying to do a search for a keyword in a file, but my output that is just coming up is "Processing file". It won't say that it's actually finding the keyword in a file.

As can be seen, I have used a preset keyword of forensics and i have told my program to look for that in a .txt file.

The problem I am getting is that it only says "Processing file"; the output won't show that any keyword has been found, which is the main aim of the project.

I can't work out where I am going wrong with this, any help would be greatly appreciated.

    package filelistingvisitor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;


public final class FileListingVisitor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String ROOT = "F:\\";
        FileVisitor<Path> fileProcessor = new ProcessFile();

        Files.walkFileTree(Paths.get(ROOT), fileProcessor);
    }

    private static final class ProcessFile extends SimpleFileVisitor<Path> {

        @Override
        public FileVisitResult visitFile(
                Path aFile, BasicFileAttributes aAttrs) throws IOException {
            System.out.println("Processing file:" + aFile);

            String fileName = aFile.toString();
            int nameLength = fileName.length() - 4;

            if (fileName.substring(nameLength, nameLength + 4) == ".txt") {
                fileScan(aFile);

            }

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(
                Path file, IOException e) throws IOException {
            System.err.printf("Visiting failed for %s\n", file);

            return FileVisitResult.SKIP_SUBTREE;
        }

        @Override
        public FileVisitResult preVisitDirectory(
                Path aDir, BasicFileAttributes aAttrs) throws IOException {
            System.out.println("Processing directory:" + aDir);
            return FileVisitResult.CONTINUE;

        }
    }

    public static void fileScan(Path aFile) throws FileNotFoundException, IOException {
        String searchterm = "forensics" ;

        Scanner scanner = new Scanner(aFile);
        while (scanner.hasNextLine()) {
            String nextToken = scanner.next();
            if (nextToken.equalsIgnoreCase(searchterm)) {
               System.out.println("Phrase Found" + searchterm + "in file" + aFile);
               break;

            }
        }


    }
}
2

There are 2 answers

0
Sionnach733 On

As claymore mentioned replacing:

fileName.substring(nameLength, nameLength + 4) == ".txt"

with:

fileName.toLowerCase().endsWith(".txt")

is a lot cleaner. You may still run into NoSuchElementException when fileScan is called. Modify the method like so:

    public static void fileScan(Path aFile) throws FileNotFoundException, IOException {
        String searchterm = "forensics" ;
        Scanner scanner = new Scanner(aFile);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.contains(searchterm)) {
                System.out.println("Phrase Found " + searchterm + " in file" + aFile);
                break;
            }
        }
        scanner.close();
    }

This will read a full line of the file and then check if "forensics" is contained in that line. Alternatively, you could replace:

while (scanner.hasNextLine()) {

with:

while (scanner.hasNext()) {

in your original method.

0
claymore1977 On

If the function

fileScan()

isn't being called from the function

visitFile()

then I would set a breakpoint on

  if (fileName.substring(nameLength, nameLength + 4) == ".txt") {

and check your logic/variables.

Also, instead of

fileName.substring(nameLength, nameLength + 4) == ".txt"

why not use something simpler like

fileName.toLowerCase().endsWith(".txt")