Java file monitoring, real-time monitoring of file loading, Commons-io already has the implementation of this feature, as shown in the following code:
Package wy.util.filemonitor;
Import Java.io.File;
Import Org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
Import Org.apache.commons.io.monitor.FileAlterationObserver;
Import Org.apache.log4j.Logger;
/**
* File Change Listener
*
* In Apache Commons-io, there is a code for the monitoring function of the file. The principle of file monitoring is as follows:
* The thread in the file monitoring class Filealterationmonitor is constantly scanning the file watcher Filealterationobserver,
* If there is a change in the file, then according to the relevant file comparator, determine whether the file is added, deleted, or changed. (the default is 1000 milliseconds to perform a scan)
*
* @author WY
*
*/
public class Filelistener extends Filealterationlisteneradaptor {
Private Logger log = Logger.getlogger (Filelistener.class);
/**
* File Creation execution
*/
@Override
public void onfilecreate (file file) {
Log.info ("[New]:" + file.getabsolutepath ());
}
/**
* File Creation Modification
*/
@Override
public void Onfilechange (file file) {
Log.info ("[Modified]:" + file.getabsolutepath ());
}
/**
* File deletion
*/
@Override
public void Onfiledelete (file file) {
Log.info ("[Delete]:" + file.getabsolutepath ());
}
/**
* Directory Creation
*/
@Override
public void Ondirectorycreate (File directory) {
Log.info ("[New]:" + directory.getabsolutepath ());
}
/**
* Directory Modification
*/
@Override
public void Ondirectorychange (File directory) {
Log.info ("[Modified]:" + directory.getabsolutepath ());
}
/**
* Directory deletion
*/
@Override
public void Ondirectorydelete (File directory) {
Log.info ("[Delete]:" + directory.getabsolutepath ());
}
@Override
public void OnStart (Filealterationobserver observer) {
TODO auto-generated Method Stub
SUPER.ONSTART (Observer);
}
@Override
public void OnStop (Filealterationobserver observer) {
TODO auto-generated Method Stub
SUPER.ONSTOP (Observer);
}
}
Package wy.util.filemonitor;
Import Java.util.concurrent.TimeUnit;
Import Org.apache.commons.io.filefilter.FileFilterUtils;
Import Org.apache.commons.io.monitor.FileAlterationMonitor;
Import Org.apache.commons.io.monitor.FileAlterationObserver;
/**
* File monitoring test
*
* In Apache Commons-io, there is a code for the monitoring function of the file. The principle of file monitoring is as follows:
* The thread in the file monitoring class Filealterationmonitor is constantly scanning the file watcher Filealterationobserver,
* If there is a change in the file, then according to the relevant file comparator, determine whether the file is added, deleted, or changed. (the default is 1000 milliseconds to perform a scan)
*
* @author WY
*
*/
public class Filemonitortest {
/**
* @param args
*/
public static void Main (string[] args) throws exception{
Monitoring Directory
String RootDir = "F:\\resume";
Polling interval 5 Seconds
Long interval = TimeUnit.SECONDS.toMillis (5);
Create a file watcher to process the file format
Filealterationobserver _observer = new Filealterationobserver (
RootDir,
Filefilterutils.and (
Filefilterutils.filefilefilter (),
Filefilterutils.suffixfilefilter (". txt")),//filter file format
NULL);
Filealterationobserver observer = new Filealterationobserver (ROOTDIR);
Observer.addlistener (New Filelistener ()); Set File Change Listener
Create a file change listener
Filealterationmonitor monitor = new Filealterationmonitor (interval, observer);
Start monitoring
Monitor.start ();
}
}
Java file change load monitoring, real-time monitoring file loading Commons-io