0

I want to watch the file on another thread and trigger method if it is updated. How can i do it on java fx. i have java code

private long timeStamp;
private File file;

public FileWatcher(File file ) {
    this.file = file;
    this.timeStamp = file.lastModified();
}

public final void run() {
    long timeStamp = file.lastModified();

    if( this.timeStamp != timeStamp ) {
        this.timeStamp = timeStamp;
        onChange(file);
    }
}

protected abstract void onChange( File file );

and in another class i can create the task

        TimerTask task = new FileWatcher(new File(classPath)) {
        protected void onChange(File file) {
            //toDoSomething
    };
    java.util.Timer timer = new Timer();
    timer.schedule(task, new Date(), 1000);

and if file will updated, method "OnChange" will trigger.

Can anyone help?

0 Answers0