aspose file tools*
The moose likes I/O and Streams and the fly likes BufferedReader fed by InputStreamReader not outputting changes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedReader fed by InputStreamReader not outputting changes" Watch "BufferedReader fed by InputStreamReader not outputting changes" New topic
Author

BufferedReader fed by InputStreamReader not outputting changes

Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
Hello,

I am trying to display in near real time the changes to a log file. I have 'proof of concept' running locally but moving my code to access a file (via a symlink) on the server has proven problematic. The symlink resides in the /var/www/html/log_links directory and points to the log file which is not under /var/www/

I have an inner class, shown below, that will grab the 'test.log' file from the server and display its contents on the console. Any changes I make to the log file as my runs are not printed out though. The only difference between this version and the version that works on my local machine is I am using a FileReader for the local file whereas I am using URL/URLConnection/InputStreamReader for the version below.

My question is how can I send the new updates to the log file down the Stream? Oh, and I only have access to Java 1.5



Appreciate the help

BD


I've got just enough Java knowledge to royally screw everything up. :-)
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1944
    
  28
Are you getting any output? I would expect (assuming the file is found) for it to print out the log file once and then do nothing else as once the reader has got to the end of the file, readline() will just return null.
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
Thanks for the response. :-)

Yes, the initial contents of the log file are displayed fine.

If I open the log file and make a change and save it as the infinite loop is running in the code above, the change is not displayed. On my local machine, using FileReader instead of the InputStreamReader stuff, the changes do show.

I'd appreciate any insight you can offer.

BD
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1944
    
  28
As I said before once you reach the end of the stream nothing else will happen. You may be able to reset the stream to the beginning, but if that fails to work you will have to open a new stream for each iteration of the loop.

BTW polling like this without some sort of delay between iterations is not a good idea.
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
Okay, I guess I wasn't following your response correctly.

I wonder if FileReader watches for changes while InputStreamReader doesn't.

Do you think my thread should sleep longer than 1 second between polls?

I'll try a reset and/or a new stream per loop.

Again, I appreciate the response and help.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1944
    
  28
Bd Howard wrote:Okay, I guess I wasn't following your response correctly.

I wonder if FileReader watches for changes while InputStreamReader doesn't.

Do you think my thread should sleep longer than 1 second between polls?

I'll try a reset and/or a new stream per loop.

Again, I appreciate the response and help.

No FileReader doesn't watch for changes, I guess your code was creating new FileReader objects on each iteration. Java 7 does have the facility to watch for file changes.

The length of the loop delay depends on how much of your computer's resources you want to tie up polling and how quickly you need to see the changes. Remember, every iteration will print the whole file again and not just the changes.
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
Using a FileReader, compiled with 1.7 I only needed to set up the reader once and it grabbed each file update just like the Unix tail command does. Notice in the code above the Reader is created outside the loop (but also remember that I use a FileReader locally, and the URL stuff in the above code). Using URLConnection and InputStreamReader compiled with 1.5 the Reader grabs the file then gives up after printing the contents once. As you mentioned, each new Reader prints out the whole file again.

How would you forward to the "new" content in the file? I am thinking about storing the "old" file size and using that to mark() with so I can skip the "old" content. That said, I have no clue if this is a good way to do it since I've never done this sort of thing. I am going to read up on the various ways to move the file pointer around but I'd appreciate any input you have regarding skipping the "old" content.

BD
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1944
    
  28
Err, I thought you said you only had access to Java 1.5?
Sorry but I've no idea what your code for reading from the file actually did - you would need to post it if you want me to comment on it.

As for keeping track of what has already been printed, you could keep a count of the number of character printed to the screen and then use the BufferedReader's skip() method to jump to the start of the new characters.
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
I'm confused. The relevant code is posted above. The only difference between my code that works locally is that instead of the URL/URLConnection/InputStreamReader I use a FileReader to grab the local log file.

Anyway, you posted a response quicker than I could get back here. I used the skip() method on the reader and it works great!!

Thanks so much for the help, I do appreciate it. You pointed me in directions I needed to be pointed.

BD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedReader fed by InputStreamReader not outputting changes
 
Similar Threads
Efficient way to download an image
How To Read Html Page Opened In Browser Using Java Program
getting error in programatically created pdf
Problem with URLConnection class
Storing last user page in DB