'read logging file in java and adding glue [closed]
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'
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
