implementation of a "FileListener"

I would like to know if someone has developped a kind of Filelistener.

Goal :

is to do something if a file has changed...

Constraints:

mustn't monopised the CPU

Else i have done a thread that is looking for the size of a file. But the problem is that it take near 100% of the CPU. Same with some Wait or Sleep Methods...

If someone have any Idea... else It could be a very good think to implement it and include it in next version of th JDK.

Thx

[505 byte] By [befa] at [2007-9-26 2:07:36]
# 1

eg?

import java.io.*;

import java.util.*;

public class FileTail extends Observable

{

private long sleepTime = 1000;

private Thread clockThread = null;

private volatile boolean noStopRequested = true;

private Runnable r = null;

private File f = null;

private long lastModified = 0;

private FileTail( String fileName )

{

f = new File( fileName );

try

{

r = new Runnable()

{

public void run()

{

try

{

runWork();

}

catch( Exception e )

{

e.printStackTrace();

}

}

};

}

catch( Exception e )

{

e.printStackTrace();

}

clockThread = new Thread( r );

clockThread.start();

}

public static void main( String[] args )

{

FileTail agt = new FileTail( args[ 0 ] );

}

public void process() {

try

{

long tn = f.lastModified();

if( 0 < ( tn - lastModified ) )

{

lastModified = tn;

System.out.println( "m: " + f.getName() );

}

}

catch( Exception e )

{

e.printStackTrace();

}

}

private void runWork() {

while( noStopRequested ) {

try {

this.process();

Thread.sleep( sleepTime );

} catch( Exception e ) {

e.printStackTrace();

}

}

}

public Object getValue()

{

return null;

}

public void stopRequest()

{

noStopRequested = false;

clockThread.interrupt();

}

public void startRequest()

{

noStopRequested = true;

clockThread.interrupt();

}

}

mchan0 at 2007-6-29 8:55:05 > top of Java-index,Archived Forums,Java Programming...