如何从特定的单词中读取java中的文件(.log文件)

时间:2017-04-25 08:43:46

标签: java file java.util.scanner

我想在java中的特定单词之后读取.log文件中的行。

文件:

Tue May 14 08:27:25 EST 2013 few words
Wed May 15 08:27:25 EST 2013 few words
Thu May 16 08:27:25 EST 2013 few words
Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words

我想阅读 Thu May 16 08:27:25 中的行,因此我需要以下几行:

Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words

2 个答案:

答案 0 :(得分:0)

这是一个简单的例子:

public static void main(String args[]) {
    try {

        List<String> lines = Files.readAllLines(new File("path\\file.txt").toPath());
        List<String> result = new ArrayList<>();

        String key = "Thu May 16 08:27:25";
        boolean start = false;
        for (String line : lines) {
            if (start) {
                result.add(line);
            }
            if (line.contains(key)) {
                start = true;
            }
        }
        for (String line : result) {
            System.out.println(line);
        }
    } catch (IOException ex) {
        //throw exception
    }
}

<强>输出

Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words

这个想法是:

  1. 获取文件的所有行List<String> lines = Files.readAllLines(file.toPath());
  2. 循环抛出你的行,并检查该行是否包含你要检查的关键字s.contains(key),开始填写结果列表
  3. 为了获得更好的性能,如果你的文件包含很多行,你可以循环抛出你的文件一次,并检查你是否也可以使用这个解决方案:

    public static void main(String args[]) {
        try {
            FileInputStream fstream = new FileInputStream("path\\file.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    
            String lines;
            String key = "Thu May 16 08:27:25";
            boolean start = false;
            List<String> result = new ArrayList<>();
    
            while ((lines = br.readLine()) != null) {
                if (start) {
                    result.add(lines);
                }
                if (lines.contains(key)) {
                    start = true;
                }
            }
            for (String line : result) {
                System.out.println(line);
            }
        } catch (IOException ex) {
            //throw exception
        }
    }
    

答案 1 :(得分:0)

在你必须从Friday读取文件时,你可以简单地检查字符串是否包含friday并在此之后打印所有字符串。

package com.stack.readfile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadLogFile {

    public static void main(String[] args) throws FileNotFoundException {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("**Path of your file**");
            br = new BufferedReader(fr);
            String sCurrentLine;
            boolean flag=false;
            while ((sCurrentLine = br.readLine()) != null) {
                if(sCurrentLine.contains("Fri")){
                    flag=true;
                }
                if(flag)
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }
    }

}

输入文件

Tue May 14 08:27:25 EST 2013 few words
Wed May 15 08:27:25 EST 2013 few words
Thu May 16 08:27:25 EST 2013 few words
Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words

<强>输出

Fri May 17 08:27:25 EST 2013 few words
Sat May 18 08:27:25 EST 2013 few words
Sun May 19 08:27:25 EST 2013 few words

希望它适合你。