-1

I have the following text file which I want to read in java on bases of string line match say on 21st May. (from 21-May-2017 12:32:30 to 21-May-2017 12:32:36)

====================abc.txt===========
20-May-2017 12:32:28 :: record logged
20-May-2017 12:32:29 :: record logged
21-May-2017 12:32:30 :: record logged
21-May-2017 12:32:31 :: record logged
21-May-2017 12:32:28 :: record logged
21-May-2017 12:32:32 :: record logged
21-May-2017 12:32:33 :: record logged
21-May-2017 12:32:34 :: record logged
21-May-2017 12:32:36 :: record logged
22-May-2017 12:32:38 :: record logged
22-May-2017 12:32:41 :: record logged
22-May-2017 12:32:42 :: record logged
22-May-2017 12:32:43 :: record logged
======================================

Is any one explain / give example for optimized way to read such chunk from big text file.

Currently I am trying with apache io package but this function also obsolete.

ReversedLinesFileReader object = new ReversedLinesFileReader(file);
String sCurrentLine ="";
while((sCurrentLine = object.readLine()) != null &&  sCurrentLine.contains("21-May-2017"))
{
    out.write(sCurrentLine);out.newLine();
}
Abdullah Khan
  • 9,469
  • 3
  • 52
  • 64
MUHIUDDIN
  • 225
  • 1
  • 2
  • 8
  • One option is to rotate your logs (one per day, one per week, one per hour, ...) so that you will not have one big log. Then the operations get simpler as you will read small log files entirely. As time goes you can move or delete old logs. – andreim May 16 '17 at 11:08
  • You can do this with `BufferedReader`, which is certainly not obsolete. Unclear why you're looking outside the JDK, or why you want to read the lines in reverse order, or why you can't reverse them yourself afterwards. There is nothing 'random' here. – user207421 May 16 '17 at 12:22

2 Answers2

0

You can try Java 8 approach:

try(Stream<String> lines = Files.lines(Paths.get(filePath))) {
    //filter lines here
}
Artyom Rebrov
  • 418
  • 4
  • 15
  • This will not read all line into list ,but lazily but instead populates lazily as the stream is consumed. – gati sahu May 16 '17 at 11:17
  • @gatisahu There is nothing in the question about reading the lines into a list. The point of your comment escapes me. – user207421 May 16 '17 at 12:24
0

If I understand correctly, ReversedLinesFileReader is used to read the file in reverse order. I do not think that is what you need unless you always expect the data to be towards the end of the file.

I would use BufferedReader instead Reading a plain text file in Java and use sCurrentLine.contains or sCurrentLine.startsWith if the string starts with the date pattern.

Community
  • 1
  • 1
rslj
  • 179
  • 2
  • 10