searching word or phrase among files I've written a program:

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Mohammad Faisal
 */
public class FileContentMatcher {

public static void main(String[] args) throws IOException {

    String textToMatch = "Quick Styles gallery on";
    ArrayList&lt;String&gt; paths = new ArrayList&lt;String&gt;();
    String content;
    int found = 0;
    int notFound = 0;
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".txt");
        }
    };

    File path = new File("E:\\anchit\\temp");
    File[] listOfFiles = path.listFiles(filter);
    for (File file : listOfFiles) {
        content = FileUtils.readFileToString(file);
        if (content.contains(textToMatch)) {
            //System.out.println("Found in: " + file.getAbsolutePath());
            paths.add(file.getAbsolutePath());
            found++;
        } else {
            //System.out.println("No found\n" + content);
            notFound++;
        }
    }
    for (String pth : paths) {
        System.out.println(pth);
    }
    System.out.println("Found in " + found + " files.\nNot found in " + notFound + " files.");
}
}


In which I've used Apache Commons IO api.
My actual requirement is to list all the files in the given directory which contains the search phrase textToMatch in minimum amount of time about 4-5 seconds, where number of files could be upto 100000. But this program takes much more time than that.
So I need to optimize this code but not getting how?
Is there any API which can help me? I've heard of Lucene but not getting how to work with it.
As poined out in the comments, your current approach might be too slow for your goal of "about 4-5 seconds". Depending on what's your actual use case, using an index might indeed be a good idea. This is similar to how internet search engines do it.

To create the index:


create a map Map&lt;String, List&lt;File&gt;&gt; for holding the association of search terms to files
go through all your files, and for each word in each file, add that file to the list corresponding to that word in your index map
you might want to skip common words, such as "a", "and", "the", etc., or you could even apply a stemmer to drastically reduce the variability in words


Once you've created the index (which could take quite some time), you just have to look up the search word in that index to get the list of files that contain the word (or a linguistic variant of it, if you used a stemmer).

As said before, the applicability of this approach depends heavily on your actual use case. If the files contain genetic sequences and you are searching a certain pattern, this probably won't help much. Even if you are searching for some complex phrase this might not work, as you'd have to add each possible (sub) phrase to the index. But if you are looking for individual words in ordinary text files (or HTML or the like) this might work.

Update: Since you seem to indeed search for complex phrases, you could try the following: 


create the index, as desribed above, optionally using a stemmer
search for each word in your phrase (or the stemmed version thereof) in your index
for each file that, according to your index, contains all the words, do a full-text search for the original phrase


Finally, if this still does not cut it, you could also create indexes for word bigrams or even trigrams.