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.

Steps:

  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.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.*;
import java.net.*;

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])) {
                                System.out.println(filename);
                                ps.println(filename);
                                System.out.println(xx);
                                ps.println(xx);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void getFiles(File folder, List list) {
        folder.setReadOnly();
        File[] files = folder.listFiles();
        for (int j = 0; j < files.length; j++) {
            list.add(files[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 *