• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading file that is updated while reading

 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a file that is being updated by another app while the reader is reading. I read to the end, then go to sleep for a couple of seconds, then look for new data. I am using BufferedReader in a while loop, and readLine() to get the data. Is there a way to read to the end of the file, then wait until the reader 'wakes up' when something new is there, rather than having to sleep?
 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brad,
How is it possible to read and update a file at the same time.I don't think its logical to do so.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered the possibility of using piped streams. They seem to be perfect for this kind of problem.

They work in different threads, one side is writing into the pipe, the other side is reading from it. If there are not bytes available, the second pipe waits until there is something available. Once there is something available it reads again.
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't ever be writing to this file. A different app is writing to the file (log file), and I need to read it in real-time. The pipe-streams are a possibility. I thought there might be a part of the I/O that handles reading in this manner, but had not found it yet.

Thanks for your suggestion.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken, the Unix "tail -f" command does exactly that. So if you're using Unix you could pipe the output of tail -f into your program.
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not Unix-specific, but thanks.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just got curious and googled for "windows tail"
People seem to be doing some work on this!
This article claims that a tail command is available as part of Windows 2003 Resource Kit!
I really was not curious enough to go through all the links but may be you can try your luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!