Apache Commons IO Tutorial
Selected Reading
- Apache Commons IO - Discussion
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - TeeInputStream
- LastModifiedFileComparator
- Apache Commons IO - SizeFileComparator
- Apache Commons IO - NameFileComparator
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileEntry
- Apache Commons IO - AndFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - NameFileFilter
- Apache Commons IO - LineIterator
- Apache Commons IO - IOCase
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - IOUtils
- Apache Commons IO - Environment Setup
- Apache Commons IO - Overview
- Apache Commons IO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache Commons IO - FileAlterationMonitor
Apache Commons IO - FileAlterationMonitor
FileAlterationMonitor 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 −
pubpc final class FileAlterationMonitor extends Object implements Runnable
Example of FileAlterationMonitor Class
Here is the input file we need to parse −
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; pubpc class IOTester { pubpc static void main(String[] args) { try { usingFileAlterationMonitor(); } catch(IOException e) { System.out.println(e.getMessage()); } } pubpc 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 pubpc void onDirectoryCreate(File file) { System.out.println("Folder created: " + file.getName()); } @Override pubpc void onDirectoryDelete(File file) { System.out.println("Folder deleted: " + file.getName()); } @Override pubpc void onFileCreate(File file) { System.out.println("File created: " + file.getName()); } @Override pubpc 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()); } } }
Output
It will print the following result.
Folder created: test File created: test1 Folder deleted: test File deleted: test1Advertisements