Monday, January 11, 2016

AUTO READER FROM TEXT FILE:



AUTO READER FROM TEXT FILE:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Timer;
import java.util.TimerTask;

public class ConsoleReader {


    public static void main(String[] args) {
        File file = new File("D:\\template_link.txt");
        try {
            RandomAccessFile r = new RandomAccessFile(file, "r");
            //First time read
            String str = null;
            while ((str = r.readLine()) != null) {
                System.out.println(str);
            }
            r.seek(r.getFilePointer());
            startTimer(r);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

    private static void startTimer(final RandomAccessFile r) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                String str = null;
                try {
                    while ((str = r.readLine()) != null) {
                        System.out.println(str);
                    }
                    r.seek(r.getFilePointer());
                } catch (IOException e) {
                }
            }
        }, 0, 1000);
    }
}

No comments:

Post a Comment