📜  Apache Commons IO-FileAlterationMonitor

📅  最后修改于: 2020-11-18 08:18:52             🧑  作者: Mango


表示一个线程,该线程以指定的时间间隔生成一个监视线程,该监视线程触发任何已注册的FileAlterationObserver。

类声明

以下是org.apache.commons.io.monitor.FileAlterationMonitor类的声明-

public final class FileAlterationMonitor
   extends Object implements Runnable

FileAlterationMonitor类的示例

这是我们需要解析的输入文件-

Welcome to TutorialsPoint. Simply Easy Learning.

IOTester.java

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileDeleteStrategy;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class IOTester {
   public static void main(String[] args) {
      try {
         usingFileAlterationMonitor();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }

   public static void usingFileAlterationMonitor() throws IOException {
      //get the file object
      File inputFile = FileUtils.getFile("input.txt");
      String absolutePath = inputFile.getAbsolutePath();
      String parent = absolutePath.substring(0,absolutePath.indexOf("input.txt"));   
      File parentDirectory = FileUtils.getFile(parent);

      FileAlterationObserver observer = new FileAlterationObserver(parentDirectory);

      observer.addListener(new FileAlterationListenerAdaptor(){

         @Override
         public void onDirectoryCreate(File file) {
            System.out.println("Folder created: " + file.getName());
         }

         @Override
         public void onDirectoryDelete(File file) {
            System.out.println("Folder deleted: " + file.getName());
         } 

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

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

      //create a monitor to check changes after every 500 ms
      FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);

      try {
         monitor.start();

         //create a new directory
         File newFolder = new File("test");
         File newFile = new File("test1");

         newFolder.mkdirs();
         Thread.sleep(1000);
         newFile.createNewFile();
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFolder);
         Thread.sleep(1000);
         FileDeleteStrategy.NORMAL.delete(newFile);
         Thread.sleep(1000);

         monitor.stop(10000);

      } catch(IOException e) {
         System.out.println(e.getMessage());
      } catch(InterruptedException e) {
         System.out.println(e.getMessage());
      } catch (Exception e) {
         System.out.println(e.getMessage());
      }
   }
}

输出

它将打印以下结果。

Folder created: test
File created: test1
Folder deleted: test
File deleted: test1