tags:

views:

335

answers:

4

I'm trying to figure out how to continuously read a file and once there is a new line added, output the line. I'm doing this using a sleep thread however it just seems to blow through the whole file and exit the program.

Any suggestions what I'm doing wrong?

Here is my code:

import java.io.*;
import java.lang.*;
import java.util.*;

class jtail { 
    public static void main (String args[])
            throws InterruptedException, IOException{ 

        BufferedReader br = new BufferedReader(
                new FileReader("\\\\server01\\data\\CommissionPlanLog.txt"));

        String line = null;
        while (br.nextLine ) {
            line = br.readLine();
            if (line == null) {
                //wait until there is more of the file for us to read
                Thread.sleep(1000);
            }
            else {
                System.out.println(line);
            }
        }
    } //end main 
} //end class jtail 

thanks in advance

UPDATE: I've since changed the line "while (br.nextLine ) {" to just "while (TRUE) {"

+1  A: 

The way your code is written now, you will not go through your while loop when your 'line==null' because you are checking to see that it has a next line before you even get into the loop.

Instead, try doing a while(true){ } loop. That way, you will always be looping through it, catching your pause cases, until you hit a condition that would cause the program to end.

James W.
A: 

As noted, your code does not parse! I am guessing in is just a file stream.

In any case, most likely your problem is here:

while (in.nextLine ) { 

This will cause you to just read the whole file and get out the while loop. You might not even get a chance to sleep.

A better option might be to

  while (true) {  
        line = br.readLine();  
        if (line == null) {  
            //wait until there is more of the file for us to read  
            Thread.sleep(1000);  
        }  
        else {  
            System.out.println(line);  
        } 
  }
Moron
actually this `nextLine` would not compile
Carlos Heuberger
@Duck: Yes, I thought I stated that already.
Moron
Since when is compiling parsing? :)
BalusC
+5  A: 

This in somewhat old, but I have used the mechanism and it works pretty well.

http://www.informit.com/guides/content.aspx?g=java&seqNum=226

The trick is to use a java.io.RandomAccessFile, and periodically check if the file length is greater that your current file position. If it is, then you read the data. When you hit the length, you wait. wash, rinse, repeat.

karoberts
+1  A: 

If you're planning to implement this on a reasonable sized application where multiple objects might be interested in processing the new lines coming to the file, you might want to consider the Observer pattern.

The object reading from the file will notify each object subscribed to it as soon as a line has been processed. This will allow you to keep logic well separated on the class where it's needed.

Dan