0

I am reading a log file line by line (using BufferedReader) & extracting data from each line. The log file has some ip, timestamp on each line & the service that was invoked at that time & the status of the call.

Sample line from the log:

10.435.234.220 - - [14/Jul/2014:08:07:07 -0700] "GET /resources/getQuote HTTP/1.1" 200 962 10.435.235.123 - - [14/Jul/2014:09:31:41 -0700] "GET /resources/getOpp HTTP/1.1" 200 962

The above is working fine.

But the next time I run the code, i want to extract data only from lines after the timestamp that was read the last time.

Say, The first time, it extracted data till the line with timestamp 14/Jul/2014:08:07:07, the next time, i want to extract data after that line. How to do this. Any help will be great.

User3
  • 69
  • 1
  • 9
  • Can you not just read and skip lines until you reach a line whose timestamp is later than the last timestamp you read? – VGR Jul 30 '14 at 19:43

1 Answers1

0

(sorry cannot comment yet)

You need to persist the state between the executions.
Just save the last line, or the line number to a file, or database.

The very next run, start reading from the line# you saved.

Here is an example of reading file from a specific line#: java: how to use bufferedreader to read specific line

Community
  • 1
  • 1
Jama Djafarov
  • 358
  • 3
  • 11
  • I am persisting the timestamp. Line number will not work because the next time, the log file could be different as new files get created after a certain size limit is reached. – User3 Jul 30 '14 at 18:26
  • I guess, it depends if you append or prepend to the log file. Well to be safe, just save the whole line, and the next run continue from there. Do you have a better solution/idea? – Jama Djafarov Jul 30 '14 at 18:55
  • There are a lot of solutions to search for a specific line in code (basically reading line by line until you find a match). Sorry,I am not sure, if I answered your question. – Jama Djafarov Jul 30 '14 at 19:17