Represents a thread that spawns a monitoring thread triggering any registered FileAlterationObserver at a specified interval.

Class Declaration

Following is the declaration for org.apache.commons.io.monitor.FileAlterationMonitor Class −

  1. public final class FileAlterationMonitor
  2. extends Object implements Runnable

Example of FileAlterationMonitor Class

Here is the input file we need to parse −

  1. Welcome to TutorialsPoint. Simply Easy Learning.

IOTester.java

  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import org.apache.commons.io.FileDeleteStrategy;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
  7. import org.apache.commons.io.monitor.FileAlterationMonitor;
  8. import org.apache.commons.io.monitor.FileAlterationObserver;
  9.  
  10. public class IOTester {
  11. public static void main(String[] args) {
  12. try {
  13. usingFileAlterationMonitor();
  14. } catch(IOException e) {
  15. System.out.println(e.getMessage());
  16. }
  17. }
  18.  
  19. public static void usingFileAlterationMonitor() throws IOException {
  20. //get the file object
  21. File inputFile = FileUtils.getFile("input.txt");
  22. String absolutePath = inputFile.getAbsolutePath();
  23. String parent = absolutePath.substring(0,absolutePath.indexOf("input.txt"));
  24. File parentDirectory = FileUtils.getFile(parent);
  25.  
  26. FileAlterationObserver observer = new FileAlterationObserver(parentDirectory);
  27.  
  28. observer.addListener(new FileAlterationListenerAdaptor(){
  29.  
  30. @Override
  31. public void onDirectoryCreate(File file) {
  32. System.out.println("Folder created: " + file.getName());
  33. }
  34.  
  35. @Override
  36. public void onDirectoryDelete(File file) {
  37. System.out.println("Folder deleted: " + file.getName());
  38. }
  39.  
  40. @Override
  41. public void onFileCreate(File file) {
  42. System.out.println("File created: " + file.getName());
  43. }
  44.  
  45. @Override
  46. public void onFileDelete(File file) {
  47. System.out.println("File deleted: " + file.getName());
  48. }
  49. });
  50.  
  51. //create a monitor to check changes after every 500 ms
  52. FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);
  53.  
  54. try {
  55. monitor.start();
  56.  
  57. //create a new directory
  58. File newFolder = new File("test");
  59. File newFile = new File("test1");
  60.  
  61. newFolder.mkdirs();
  62. Thread.sleep(1000);
  63. newFile.createNewFile();
  64. Thread.sleep(1000);
  65. FileDeleteStrategy.NORMAL.delete(newFolder);
  66. Thread.sleep(1000);
  67. FileDeleteStrategy.NORMAL.delete(newFile);
  68. Thread.sleep(1000);
  69.  
  70. monitor.stop(10000);
  71.  
  72. } catch(IOException e) {
  73. System.out.println(e.getMessage());
  74. } catch(InterruptedException e) {
  75. System.out.println(e.getMessage());
  76. } catch (Exception e) {
  77. System.out.println(e.getMessage());
  78. }
  79. }
  80. }

Output

It will print the following result.

  1. Folder created: test
  2. File created: test1
  3. Folder deleted: test
  4. File deleted: test1