Monitor disk File change codes

Source: Internet
Author: User
Tags iterable

File Listener

public class Filelistener extends filealterationlisteneradaptor{
Log operations
private static final Logger Logger = Loggerfactory.getlogger (Filelistener.class);
@Override
public void Ondirectorychange (File directory) {
SYSTEM.OUT.PRINTLN ("File directory changed:" +directory.getabsolutepath ());
Pass directory to Folder scan method
Querydata (Directory.getabsolutepath ());
}

@Override
public void Ondirectorycreate (File directory) {
SYSTEM.OUT.PRINTLN ("File directory created:" +directory.getabsolutepath ());
}

@Override
public void Ondirectorydelete (File directory) {
SYSTEM.OUT.PRINTLN ("File directory deleted:" +directory.getabsolutepath ());
}

@Override
public void Onfilechange (file file) {
System.out.println ("File changed:" +file.getabsolutepath ());
}

@Override
public void onfilecreate (file file) {
System.out.println ("File created:" +file.getabsolutepath ());
}

@Override
public void Onfiledelete (file file) {
System.out.println ("File deleted:" +file.getabsolutepath ());
}

@Override
public void OnStart (Filealterationobserver observer) {
/* Logger.info ("File system watcher begins to check events"); */
SYSTEM.OUT.PRINTLN ("Start monitoring:" +observer.getdirectory ());
}

@Override
public void OnStop (Filealterationobserver observer) {
/*logger.info ("File system Watcher stops checking events"); */
SYSTEM.OUT.PRINTLN ("Stop Monitoring:" +observer.getdirectory ());
}

private void Querydata (String path) {
Scan the data under the change path
arraylist<string> findfilename = findfilename (path);
Create a CXF service
jaxwsdynamicclientfactory DCF = Jaxwsdynamicclientfactory.newinstance ();
Create a client by WebService service address
String variablewsdl=variableservice.selectvariable ("Ws_wsdlmonitor");
Client client = dcf.createclient (utils.returnsacndate);
if (0==findfilename.size ()) {
Findfilename.add ("null");//When he is empty in order to avoid the error of empty, so we set a null in this
}
try {
Client.invoke ("Returnscandate", path,findfilename);
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("Webserver Wait a long time, no response!") ");
}
}

Find files under the specified directory
Private arraylist<string> FindFileName (String filepath) {
File File = new file (filepath);
arraylist<string> list = new arraylist<string> ();
if (!file.exists () | |!file.isdirectory ()) {
SYSTEM.OUT.PRINTLN ("Directory does not exist");
} else {
file[] files = file.listfiles ();
for (int i = 0; i < files.length; i++) {
if (Files[i].isfile ()) {
if (Files[i].getname (). EndsWith (". jpg")) {
List.add (Files[i].getname ());
}
}
}
}
return list;
}
}

Package Cn.bocai.pc.FileMonitorMethod;

Import Org.apache.commons.io.monitor.FileAlterationMonitor;
Import Org.apache.commons.io.monitor.FileAlterationObserver;

/**
* Monitor class.
*
*/
public class FileMonitor {

Filealterationmonitor monitor = null;

/**
* Default 8 min to watch once
* @param ob
*/
Public FileMonitor (Filealterationobserver ob) {
This (480000,OB);
This (30000,OB);
}

/**
* Once every few times, the Observer
* @param fileName
* @param ob
*/
Public FileMonitor (Long interval,filealterationobserver ob) {
Monitor = new Filealterationmonitor (Interval,new filealterationobserver[]{ob});
}

/**
* Add Observer
* @param Observer
*/
public void Addobserver (Filealterationobserver observer) {
MONITOR.ADDOBSERVER (Observer);
}

/**
* Remove Viewer
* @param Observer
*/
public void Removeobserver (Filealterationobserver observer) {
MONITOR.REMOVEOBSERVER (Observer);
}

/**
* Get all observers
* @return
*/
Public iterable<filealterationobserver> getobservers () {
return Monitor.getobservers ();
}

/**
* Start monitoring [Observer.initialize ()]
*/
public void Start () {
try {
Monitor.start ();
} catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Stop monitoring [Observer.destroy ()]
*/
public void Stop () {
try {
Monitor.stop ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

/**
* [Do not call]
* Specific monitoring operation:
* Observer.checkandnotify ()
*/
private void Run () {
Monitor.run ();
}


}

Package Cn.bocai.pc.FileMonitorMethod;

Import Java.io.File;
Import Java.io.FileFilter;
Import Org.apache.commons.io.IOCase;
Import Org.apache.commons.io.monitor.FileAlterationListener;
Import Org.apache.commons.io.monitor.FileAlterationObserver;

public class Fileobserver extends filealterationobserver{

Private static final long serialversionuid = 3637219592248717850L;

/**
* Set the viewing File object/Path object
* @param directory
*/
Public fileobserver (String directory) {
This (new File (directory), (filefilter) null);
}

/**
* Set the view file path with a file filter, such as all XML files.
* The iocase here is the set file comparator (sort). [Depending on the system, you can ignore]
* @param directoryname
* @param filefilter
*/
Public Fileobserver (File fileName, FileFilter filefilter) {
Super (Filename,filefilter, (iocase) null);
}

/**
* Monitor initialization method at startup
* 1. Refresh this file object
* 2. Get a list of this file
* 3. Looping through the initialization of a file entity
* 4. Set up this document and the subordinate relationship under it
*/
public void Initialize () throws Exception {
Super.initialize ();
}

/**
* Stop Monitoring
* Nothing is done by default
*/
public void Destroy () throws Exception {
Super.destroy ();
}


/**
* Monitoring files have not been created, modified, deleted
* and trigger the corresponding monitoring
* If the file name is larger than the original, create an entity and call Ondirectorycreate/onfilecreate
* If the file name is smaller than the original, then delete an entity and call Ondirectorydelete/onfiledelete
* If the file name is the same as the plaintiff's, the new file is compared to the original file, and if the file attributes are changed and called Ondirectorychange/onfilechange, the loop calls itself, possibly the folder
* If the name length is queued, add the file and call Ondirectorycreate/onfilecreate
* This registers the listener for all files
*/
public void Checkandnotify () {
Super.checkandnotify ();
}

/**
* Add listener
*/
public void AddListener (final Filealterationlistener listener) {
Super.addlistener (listener);
}

/**
* Remove Listener
*/
public void RemoveListener (final Filealterationlistener listener) {
Super.removelistener (listener);
}


/**
* Get all listeners for the Observer object
*/
Public iterable<filealterationlistener> getlisteners () {
return Super.getlisteners ();
}

}

Monitor disk File change codes

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.