-2

My src code is generating a log file. My BDD scenario no 10 is asserting whether a log message " ERROR Error handling a request:" is present for file X. Now this log message can be generated for other files as well from scenario that run previously . 'Scenario 1 :- Then log message should have "abcdef" printed to the sft agent log file Scenario 2 :- Then log message should have "gjhabcdefg" printed to the sft agent log file'

aron
  • 17
  • 1
  • 4
  • The Apache FileUtils has a good solution for this, see the accepted answer here: https://stackoverflow.com/questions/2138390/reading-a-specific-line-from-a-text-file-in-java – sorifiend Mar 14 '22 at 23:22
  • The edited version of this question doesn't contain an actual question, and contains unexplained acronyms and abbreviations, and seems to be an implied request for someone to find or write some code for you. And it bears little relation to the question title. (And it seems to be asking a substantially different question to the original version.) – Stephen C Mar 30 '22 at 10:26

1 Answers1

1

I am generating a log file and what i want is that i want to read the data periodically without having to read from the beginning each time

I assume you mean you want to carry on reading from where you left off before.

If you keep the file open, then obviously the next place to read is just after the previous place you have read.

If you want to close the file (in the reader) then use the RandomAccessFile class, remember your position (getFilePointer), and seek to that position when you re-open the file.

In this case, you'll need to take precautions that it's "the same file" that you read previously. With log files, it's typical to close one log and open the next. To detect that, maybe check the create time, make sure the size doesn't decrease, etc. (I don't know if you can get the inode number via Java).

For a simpler solution, if you don't actually mind reading from the beginning and just want to be able to process new data, then remember the line number that you read up to before, and skip over that many lines the next time you open the file.

You'll still want to take precautions to make sure it's the same log file.

passer-by
  • 1,103
  • 2
  • 4