String path = "c:/test";
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED|
JNotify.FILE_RENAMED;
boolean watchSubtree = true;
int watchID = JNotify.addWatch(path, mask, watchSubtree, new JNotifyListener()
{
public void fileRenamed(int wd, String rootPath, String oldName, String newName)
{
System.out.println("JNotifyTest.fileRenamed() : wd #" + wd + " root = " + rootPath + ", " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name)
{
System.out.println("JNotifyTest.fileModified() : wd #" + wd + " root = " + rootPath + ", " + name);
}
public void fileDeleted(int wd, String rootPath, String name)
{
System.out.println("JNotifyTest.fileDeleted() : wd #" + wd + " root = " + rootPath
+ ", " + name);
}
public void fileCreated(int wd, String rootPath, String name)
{
System.out.println("JNotifyTest.fileCreated() : wd #" + wd + " root = " + rootPath
+ ", " + name);
}
});
// to remove watch:
boolean res = JNotify.removeWatch(watchID);
if (!res)
{
// invalid watch ID specified.
}
硬盘监控在很多场合都很有用处,例如在线文档转换,只要监控到有文件写入,即可触发在线文档转换程序。JNotify支持windows和Linux两大操作系统,并且支持64位的机器,在使用JNotify时,需要手动将JNotify.dll或者libjnotify.so复制到JRE的bin目录下,然后在你的JAVA工程编写如下代码: