Java file change load monitoring, real-time monitoring file loading Commons-io

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.