• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read a file to watch it

 
Pramod Pateel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a unique prolem where, i have to monitor a file(lets' call this file "fileabc") continuously. A lot of other processes are writing to this file. The file grows by the second. I want to read "fileabc" continuously and search for a pattern. once I reach the end of the file "fileabc" do I have to reread the file to see if and when it has changed? or is there a way I can keep on reading the file?? Thanks in advance for your help.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it should be possible, to try to read further when on end of file after a short delay.
On linux we have a program for that job, 'tail' which shows the end of a file, and can wait for it growing.

Why shouldn't it be possible with java?
Why not on your platform?
[ September 08, 2004: Message edited by: Stefan Wagner ]
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a very rudimentary example of how this could work:

There's a lot wrong with it but it demonstrates the idea. The biggest issue IMO is deciding when the end of the input file has actually been reached. I wondered if it might be possible to used PipedInputStream and PipedOutputStream for a more elegant implementation but I've never used them and I don't even know if they'd be appropriate.

Hope this helps.

Jules
 
Pramod Pateel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks to both Stefan and Jules for the quick reply.

Stefan,
Tail's functionality is exactly what I am trying to simulate in Java.

Jules,
Thanks for the code. Determining the end of the file is not a problem as the program is going to run for ever until the user interrupts the program (just like in tail -f). To provide the continuous read functionality, interrupt from the user is necessary as the program will not know to when end.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm probably oversimplifying this, but can't you do something like the below, instead of constantly reading the file only read it if it's size has changed maybe

 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a BufferedReader, readLine() returns null when it reaches end-of-file. Just ignore the eof and read again:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, neat, Joe. I was thinking you could use random access. Read to end, close, remember the size. Wait a while, open, seek the prior size, read to end, close. Rinse, repeat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!