Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Java Example of org.apache.commons.io.monitor
Where can I find an example Java code that uses the org.apache.commons.io.monitor package?
✍: FYIcenter.com
org.apache.commons.io.monitor package provides a component for monitoring
file system events (directory and file create, update and delete events).
Andrei Ciobanu provided a good Java example that uses the org.apache.commons.io.monitor package at andreINC.net Website.
// Source: andreINC.net Website
// Supports commons-io-2.6
// Supports commons-io-2.5
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
public class SimpleTestMonitor {
// A hardcoded path to a folder you are monitoring .
public static final String FOLDER = "/temp";
public static void main(String[] args) throws Exception {
// The monitor will perform polling on the folder every 5 seconds
final long pollingInterval = 5 * 1000;
File folder = new File(FOLDER);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
@Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
// Is triggered when a file is deleted from the monitored folder
@Override
public void onFileDelete(File file) {
try {
// "file" is the reference to the removed file
System.out.println("File removed: "
+ file.getCanonicalPath());
// "file" does not exists anymore in the location
System.out.println("File still exists in location: "
+ file.exists());
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
}
}
You can compile and run the above example in a command window as shown below:
C:\fyicenter>javac -cp C:\fyicenter\commons-io-2.6\commons-io-2.6.jar SimpleTestMonitor.java C:\fyicenter>java -cp .;C:\fyicenter\commons-io-2.6\commons-io-2.6.jar SimpleTestMonitor
Then copy and paste the Java code file to the C:\temp folder. You see a new message displayed in the command window:
File created: C:\temp\SimpleTestMonitor.java
⇒ More Java Example on Apache Commons IO JAR
⇐ Java Example of org.apache.commons.io.comparator
2017-04-28, ∼3898🔥, 0💬
Popular Posts:
Saxon-HE (home edition) is an open source product available under the Mozilla Public License. It pro...
What Is jaxb-api-2.1.6.jar? Java Architecture for XML Binding (JAXB) is a Java API that allows Java ...
commons-net.jar is the bytecode of Apache Commons Net library, which implements the client side of m...
How to download and install ojdbc7.jar for Oracle 12c R1? ojdbc8.jar for Oracle 12c R1 is a Java 7 a...
JDK 11 jdk.jshell.jmod is the JMOD file for JDK 11 JShell tool, which can be invoked by the "jshell"...