简体   繁体   中英

Search for specific string inside text file

I am currently developing a java application that will read the contents of text files from a specific folder. The application is already capable of reading the contents of text files, however, the text files are server logs and I only need to take data from their contents such as Error Name, Time, Date, etc. Any ideas on how to do that, or functions that can help me?

For a better view here is my code.

public class ListFiles {

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

        // Directory path here
        String path = "C:/Users/Pasusani/Desktop/logs";

        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {

            if (listOfFiles[i].isFile()) {
                files = listOfFiles[i].getName();
                if (files.endsWith(".txt") || files.endsWith(".TXT")) {
                    if (listOfFiles[i].getName().equals("R320-txtfile.txt")) {
                        JOptionPane.showMessageDialog(null, listOfFiles[i]
                                .getName().toString());

                        String paths = "C:/Users/Pasusani/Desktop/logs/"
                                + listOfFiles[i].getName().toString();
                        FileReader file = new FileReader(paths);
                        BufferedReader reader = new BufferedReader(file);

                        String text = "";
                        String line = reader.readLine();
                        while (line != null) {
                            text += line;
                            line = reader.readLine();
                        }
                        System.out.println(text);
                        JTextArea ta = new JTextArea();
                        ta.setVisible(true);
                        ta.setText(text);

                    } else if (listOfFiles[i].getName().equals("try.txt")) {
                        String paths = "C:/Users/Pasusani/Desktop/logs/"
                                + listOfFiles[i].getName().toString();
                        FileReader file = new FileReader(paths);
                        BufferedReader reader = new BufferedReader(file);

                        String text = "";
                        String line = reader.readLine();
                        while (line != null) {
                            text += line;
                            line = reader.readLine();
                        }
                        System.out.println(text);
                        JTextArea ta = new JTextArea();
                        ta.setVisible(true);
                        ta.setText(text);

                    }

                    else {
                        JOptionPane.showMessageDialog(null, "hah");
                    }

                    // System.out.println(files);
                } else {
                    JOptionPane.showMessageDialog(null, "wala lang");
                }
            }
        }
    }

}

I think your program can successfully output the log in the JTextArea, right?

You need to add statements to deal with the text getting from the Bufferreader according to the structure of your log file. We cannot help you without the knowledge of your log files.

You may need to use the contains(String str), substring(int beginIndex,int endIndex) and indexOf(String str) of String. A simple sample:

    public static String testMethod(){

    String a = "hello error";
    if(a.contains("error"))
        return a.substring(a.indexOf("error"), a.indexOf("error")+5);
    else
        return "not found";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM