Ahmad Abu-Hamideh
Ahmad Abu-Hamideh

Reputation: 253

Java , read text file continuously is not working

i have used the following method to read lines from a TEXTFILE , whenver it is updated or new lines inserted to the textfile i must print that new line

searching inside this site give me the idea but it is not working

FileReader fr = new FileReader("filename.txt");
BufferedReader reader = new BufferedReader(fr);
while (true) {
    String line = reader.readLine();
    if (line != null) {
        // Use line
    }
    else {
        thread.sleep(1000);
    }
}

what i am doing wrong ?

Note : i need to capture the new lines that appended to the Text file while the loop is running , i hope that it is clear to you

Upvotes: 0

Views: 658

Answers (4)

Ming
Ming

Reputation: 358

In Linux,

File are actually inode. Your old file probably got deleted while saving using gedit. For eg:

At first, you have filename.txt with inode number 10. And the java is reading it. But when you save edit with gedit. The file probably get replace by filename.txt with inode number 11. But the Java application is still reading the inode 10. So the application won't able to see any change. You can try use "ls -i" to find out the inode number of your file. See if it changed after edit.

Upvotes: 1

DJSweetness
DJSweetness

Reputation: 153

The only thing I can think of is that your file isn't saving when the changes are made so your code never registers any new lines. Also, it probably still has the original file open while you're in the loop so it never opens the new file with the changes either. Try putting the open file in the while loop and make sure to close the file too.

Upvotes: 0

tomas
tomas

Reputation: 840

You can use the apache commons class "org.apache.commons.io.input.Tailer".

https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/input/Tailer.html

  TailerListener listener = new MyTailerListener();
  Tailer tailer = new Tailer(file, listener, delay);

  // simple executor impl. for demo purposes
  Executor executor = new Executor() {
      public void execute(Runnable command) {
          command.run();
       }
  };

  executor.execute(tailer);

For example

class MyTailerListener extends TailerListenerAdapter {
public void handle(String line) {
  System.out.println(line);
}
}
TailerListener listener = new MyTailerListener();
Tailer tailer = Tailer.create(file, listener, 10000);
tailer.stop()

Upvotes: 2

Sergey Gornostaev
Sergey Gornostaev

Reputation: 7787

import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Tailer implements Runnable {
    private File file;
    private int runEvery;
    private long lastPosition = 0;
    private boolean run = true;

    public Tailer(String inputFile, int interval) {
        file = new File(inputFile);
        this.runEvery = interval;
    }

    public void stop() {
        run = false;
    }

    public void run() {
        try {
            while(run) {
                Thread.sleep(runEvery);
                long fileLength = file.length();
                if(fileLength > lastPosition) { 
                    RandomAccessFile fh = new RandomAccessFile(file, "r");
                    fh.seek(lastPosition);

                    byte c;
                    while((c = (byte)fh.read()) != -1) {
                        System.out.print((char)c);
                    }
                    lastPosition = fh.getFilePointer();
                    fh.close();
                }
            }
        }
        catch(Exception e) {
            stop();
        }
    }

    public static void main(String argv[]) {
        ExecutorService executor = Executors.newFixedThreadPool(4);
        Tailer tailer = new Tailer("test.log", 1000);
        executor.execute(tailer);
    } 
}

Upvotes: 2

Related Questions