Java program to search multiple strings in multiple files

We usually use search option in text editors to search a particular keyword or string in a file. We also sometime search the keyword or string in multiple files at a time using some text editors.

But we cannot search multiple strings in multiple files at a time using the text editors.

We may need to do such multiple string search in multiple files in a single sort in some situations like while analyzing some files.

Below program helps you to do such search.


  1. Put the files to be searched in a specific folder. Below code is mapped to “D:\filestosearch” folder. You can change the folder as your need.
  2. Add the strings to be searched in the string array “searchstringslist”.
  3. Run the code.
  4. Results will be displayed in console and also it will be stored in “D:\log.txt” file.

Note: Below code will search only in .properties files. This if condition can be removed to search all files. Or the same can be modified as “.txt” to search in text files.

import java.util.*;

public class search {

    public static void main(String[] args) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        PrintStream ps = null;
        String xx;
        String[] searchstringslist = { "10521", "18521"};
        try {
            fos = new FileOutputStream(new File("D://log.txt"));
            ps = new PrintStream(fos);
            File folder = new File("D://filestosearch");
            List list = new ArrayList();
            getFiles(folder, list);
            System.out.println("list.size = " + list.size());
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).toString().endsWith(".properties")) {
                    URL url = list.get(i).toURL();
                    String filename = url.getFile();
                    fis = new FileInputStream(new File(filename));
                    bis = new BufferedInputStream(fis);
                    dis = new DataInputStream(bis);
                    while (dis.available() != 0) {
                        xx = dis.readLine();
                        for (int j = 0; j < searchstringslist.length; j++) {
                            if (xx.contains(searchstringslist[j])) {
        } catch (Exception e) {
    private static void getFiles(File folder, List list) {
        File[] files = folder.listFiles();
        for (int j = 0; j < files.length; j++) {
            if (files[j].isDirectory())
                getFiles(files[j], list);

Note: Double click anywhere within the code to select the full code snippet.

Leave a Reply

Your email address will not be published. Required fields are marked *