Today saw a netizen write Java file monitoring, real-time monitoring file loading, suddenly think of commons-io in the implementation of this function, first review to write a simple demo:
There are three ways of doing this:1, Java Common.io Internal implementation is the way of traversal, small folder efficiency is good, such as I test 60G directory, very slow very slow. (I use this)2. The Watch service for JDK 7//tested is basically not available. In a deep 40g directory, the new and deleted files have no effect for 5 minutes. The main reason is that each path needs to be registered for monitoring. 3, jnotify directly call the Windows API, high efficiency, also very simple, recommended to use. Common.iorequires Java.io 2.1 and later. The version address is as follows:http://commons.apache.org/io/download_io.cgiJava 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);
- }
- }
Java code
- 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 file Change listener
- Filealterationmonitor monitor = new Filealterationmonitor (interval, observer);
- //Start monitoring
- Monitor.start ();
- }
- }
Look at the results:
==============================================================================
The Netizen's post: http://www.iteye.com/topic/1127281
There is also a netizen's post: http://dyccsxg.iteye.com/blog/618993
Two-bit ideas are very similar, basically using thread polling, but in verifying the file update when a file check code and hash code, the other is the last modification time of the file (LastModified ()).
Java file Real-time monitoring Commons-io