1

I am new to Java. I am supposed to read an ever growing file for new data being appended to it. On googling I could find out a solution using RandomAccessFile, is that the best way? Can you suggest an optimal way that would maintain the balance between speed/performance and accuracy?

I am not expecting ready to use code, but suggestion from you experts out there.

ADJ
  • 1,182
  • 2
  • 14
  • 26
  • What's wrong with just keeping a FileInputStream open on it and continuing to try to read from it periodically? – keshlam Jan 29 '14 at 10:56
  • Nothing wrong, but I am not sure whether it is the best way..? – ADJ Jan 29 '14 at 10:57
  • This will be a good start : http://skillshared.blogspot.in/2012/11/how-to-read-dynamically-growing-file.html – Egalitarian Jan 29 '14 at 10:57
  • Yes, I had seen this code, my question is whats the best way? – ADJ Jan 29 '14 at 10:57
  • The best way will be: create unit test, implement what you have to, looking for a better way IF it will be not enough. Look http://en.wikipedia.org/wiki/Worse_is_better – zimi Jan 29 '14 at 11:02
  • if it is a file created by you(custom file) or you can make changes in it then probably add some constant update statement before appending new text. while retrieving find last occurance of constant update statement and from that point till end of file is reached is your required text – Nachiket Kate Jan 29 '14 at 13:21
  • Is this a real requirement or you wanted to write something for learning purpose ? If the former, then why not use tail -f or UNIX or Get-Content -Wait on Windows ? If the latter, then http://stackoverflow.com/questions/3791103/c-sharp-continuously-read-file would help. That's in C# though. You can apply the same idea in Java. – Jagannath Jan 30 '14 at 06:12
  • @Nachiket: No its a file generated by some other component. – ADJ Feb 04 '14 at 05:22
  • @Jagannath: Yes, its a real requirement where in I am planning to read a log file in Java as and when it changes, but I am not sure what would be the efficient way, hence this question. – ADJ Feb 04 '14 at 05:24
  • still if possible can you make changes in that code which allows you to add some custom log statements in file and then read it using tail -f or sed (find last occurance of log stmt and read till end of file) – Nachiket Kate Feb 04 '14 at 05:27
  • As I said earlier the C# implementation could be a good start. – Jagannath Feb 05 '14 at 05:49

0 Answers0