Best way to search many strings in huge files at once

Viewed 66

I want to search for 10 strings in huge set of files. What would be the best way to do it?

1. Using index of each string to be find in file as string
2. Using contains method of string to find the given each string
3. Using split method of string to find the given each string

Currently reading the file as string and using indexOf of each string.

    public static int countMatchesWithIndexOf(String text, String str)
{
    int index = 0, count = 0;
    while (true)
    {
        index = text.indexOf(str, index);
        if (index != -1)
        {
            count ++;
            index += str.length();
        }
        else {
            break;
        }
    }

    return count;
}

    static String readFile(String path)
        throws IOException
{
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, StandardCharsets.UTF_8);
}

Is there a way to find the occurrences of each string in a file in a better way to do this?

Assume: Keyword to search - "Emp Id: 124", "Emp Name: Raj", "Emp Id:587", "Entity Not Found", "File Not Found", "Exception Occured", "System Error"

Now i want to search each of the string and the number of occurences in each file as well as overall count in huge log files which is in a directory or sub-directories..

0 Answers
Related