import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
public class FileDirMonitor extends Thread{
public static void main(String[] args) {
FileDirMonitor fd = new FileDirMonitor();
try{
System.out.println("main:"+Thread.currentThread().getName());
fd.sample();
}catch(Exception e){
}
}
public void sample() throws Exception {
try{
System.out.println("sample:"+Thread.currentThread().getName());
// path to watch
String path = "D:\\work\\RA\\2012\\0111";
String path2 = "E:\\";
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
int watchID2 = JNotify.addWatch(path2, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
//休眠被唤醒时,捕获该异常,不做额外处理,继续后续操作
}
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
boolean res2 = JNotify.removeWatch(watchID2);
}catch(Exception e){
System.out.println("error:"+e.getMessage());
}
}
}
class Listener extends Thread implements JNotifyListener {
public Listener() {
System.out.println("new thread:"+Thread.currentThread().getName());
}
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
System.out.println("renamed thread:"+Thread.currentThread().getName());
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
System.out.println("modified thread:"+Thread.currentThread().getName());
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
System.out.println("deleted thread:"+Thread.currentThread().getName());
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
System.out.println("created thread:"+Thread.currentThread().getName());
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
public class FileDirMonitor extends Thread{
public static void main(String[] args) {
FileDirMonitor fd = new FileDirMonitor();
try{
System.out.println("main:"+Thread.currentThread().getName());
fd.sample();
}catch(Exception e){
}
}
public void sample() throws Exception {
try{
System.out.println("sample:"+Thread.currentThread().getName());
// path to watch
String path = "D:\\work\\RA\\2012\\0111";
String path2 = "E:\\";
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
int watchID2 = JNotify.addWatch(path2, mask, watchSubtree, new Listener());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
//休眠被唤醒时,捕获该异常,不做额外处理,继续后续操作
}
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
boolean res2 = JNotify.removeWatch(watchID2);
}catch(Exception e){
System.out.println("error:"+e.getMessage());
}
}
}
class Listener extends Thread implements JNotifyListener {
public Listener() {
System.out.println("new thread:"+Thread.currentThread().getName());
}
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
System.out.println("renamed thread:"+Thread.currentThread().getName());
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
System.out.println("modified thread:"+Thread.currentThread().getName());
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
System.out.println("deleted thread:"+Thread.currentThread().getName());
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
System.out.println("created thread:"+Thread.currentThread().getName());
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}