-2

I want to read the lines from .log file after the specific word in java.

File :

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

I want read the lines from Thu May 16 08:27:25, as a result I need the below lines :

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
YCF_L
  • 49,027
  • 13
  • 75
  • 115
Alagammal P
  • 479
  • 3
  • 11
  • 34
  • 1
    Can't you use the `grep -A 2 "Thu May 16 08:27:25" "file"` command? – Ferrybig Apr 25 '17 at 08:46
  • 1
    Then read [this](http://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java) and start writing code. But Ferrybig is fully correct: if you are just interested in fetching a line from that file; use tools like grep. – GhostCat Apr 25 '17 at 08:46
  • 1
    this does not really show research effort – Lukas A Apr 25 '17 at 08:46
  • Applications should not read application log files. They are for humans, and ultimately for judges and juries. If you need an event system, use an event system, or a database. – user207421 Apr 25 '17 at 09:53

2 Answers2

0

Here is a simple example :

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
    }
}

Output

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

The idea is :

  1. Get all the lines of your files List<String> lines = Files.readAllLines(file.toPath());
  2. Loop throw your lines, and check if the line contain the key word s.contains(key) you want to check, start to fill your result List

For better performance and if your file contain a lot of lines, you can loop throw your file just one time, and check you can use also this solution :

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
    }
}
YCF_L
  • 49,027
  • 13
  • 75
  • 115
0

In your you have to read the file from Friday, then you can simply check whether string contains friday and print all the string after that.

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();

            }

        }
    }

}

Input File

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

Output

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

Hope it works for you.

Anuj Teotia
  • 1,155
  • 1
  • 12
  • 21