Java file Real-time monitoring Commons-io

Source: Internet
Author: User

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
  1. Package wy.util.filemonitor;
  2. Import Java.io.File;
  3. Import Org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
  4. Import Org.apache.commons.io.monitor.FileAlterationObserver;
  5. Import Org.apache.log4j.Logger;
  6. /**
  7. * File Change Listener
  8. *
  9. * In Apache Commons-io, there is a code for the monitoring function of the file. The principle of file monitoring is as follows:
  10. * The thread in the file monitoring class Filealterationmonitor is constantly scanning the file watcher Filealterationobserver,
  11. * 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)
  12. *
  13. * @author WY
  14. *
  15. */
  16. Public class Filelistener extends Filealterationlisteneradaptor {
  17. private Logger log = Logger.getlogger (Filelistener.   Class);
  18. /** 
  19. * File Creation execution
  20. */
  21. @Override
  22. public void onfilecreate (file file) {
  23. Log.info ("[New]:" + file.getabsolutepath ());
  24. }
  25. /** 
  26. * File Creation Modification
  27. */
  28. @Override
  29. public void Onfilechange (file file) {
  30. Log.info ("[Modified]:" + file.getabsolutepath ());
  31. }
  32. /** 
  33. * File deletion
  34. */
  35. @Override
  36. public void onfiledelete (file file) {
  37. Log.info ("[delete]:" + file.getabsolutepath ());
  38. }
  39. /** 
  40. * Directory Creation
  41. */
  42. @Override
  43. public void Ondirectorycreate (File directory) {
  44. Log.info ("[New]:" + directory.getabsolutepath ());
  45. }
  46. /** 
  47. * Directory Modification
  48. */
  49. @Override
  50. public void Ondirectorychange (File directory) {
  51. Log.info ("[Modified]:" + directory.getabsolutepath ());
  52. }
  53. /** 
  54. * Directory deletion
  55. */
  56. @Override
  57. public void Ondirectorydelete (File directory) {
  58. Log.info ("[delete]:" + directory.getabsolutepath ());
  59. }
  60. @Override
  61. public void OnStart (Filealterationobserver observer) {
  62. //TODO auto-generated method stub
  63. Super.onstart (Observer);
  64. }
  65. @Override
  66. public void OnStop (Filealterationobserver observer) {
  67. //TODO auto-generated method stub
  68. SUPER.ONSTOP (Observer);
  69. }
  70. }
Java code
  1. Package wy.util.filemonitor;
  2. Import Java.util.concurrent.TimeUnit;
  3. Import Org.apache.commons.io.filefilter.FileFilterUtils;
  4. Import Org.apache.commons.io.monitor.FileAlterationMonitor;
  5. Import Org.apache.commons.io.monitor.FileAlterationObserver;
  6. /**
  7. * File monitoring test
  8. *
  9. * In Apache Commons-io, there is a code for the monitoring function of the file. The principle of file monitoring is as follows:
  10. * The thread in the file monitoring class Filealterationmonitor is constantly scanning the file watcher Filealterationobserver,
  11. * 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)
  12. *
  13. * @author WY
  14. *
  15. */
  16. Public class Filemonitortest {
  17. /** 
  18. * @param args
  19. */
  20. public static void Main (string[] args) throws exception{
  21. //Monitoring directory
  22. String RootDir = "F:\\resume";
  23. //Polling interval 5 seconds
  24. Long interval = TimeUnit.SECONDS.toMillis (5);
  25. //Create a file watcher to process the file format
  26. Filealterationobserver _observer = new Filealterationobserver (
  27. RootDir,
  28. Filefilterutils.and (
  29. Filefilterutils.filefilefilter (),
  30. Filefilterutils.suffixfilefilter (". txt")), //filter file format
  31. null);
  32. Filealterationobserver observer = new Filealterationobserver (RootDir);
  33. Observer.addlistener (new Filelistener ()); //Set file change listener
  34. //Create file Change listener
  35. Filealterationmonitor monitor = new Filealterationmonitor (interval, observer);
  36. //Start monitoring
  37. Monitor.start ();
  38. }
  39. }

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

Related Article

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.