Java中的目录侦听器 [英] Directory listener in Java
问题描述
我有一个应用程序,我想听取对特定目录所做的任何更改。应用程序应该在该目录中添加,删除或更新任何文件后立即ping我。
可以使用< a href =http://jnotify.sourceforge.net/ =noreferrer> JNotify
JNotify是一个允许java应用程序监听文件
系统事件的java库,如:文件创建
文件修改文件重命名文件
已删除支持平台
$ b $ Windows(2000或更新版本)Windows注释
具有INofity支持的Linux(2.6.14或
更新)Linux说明Mac OS X(10.5或
较新)Mac OS笔记
更多信息
从JNotify从< a href =http://sourceforge.net/projects/jnotify/files/jnotify/jnotify-0.93/jnotify-lib-0.93.zip/download =noreferrer> here
根据平台在lib路径中提取zip,放入.dll / .so。并在类路径中创建一个类,提供 jnotify-0.93.jar
。
示例代码:
package org.life.java.stackoverflow .questions;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
/ **
*
* @author Jigar
* /
public class JNotifyDemo {
public void sample )throws Exception {
// path to watch
String path = System.getProperty(user.home);
//监视掩码,指定关心的事件,
//或JNotify.FILE_ANY用于所有事件。
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 Listener());
//睡一会儿,如果
//不要(看异步),应用程序将退出,取决于您的
//应用程序,这可能不是必需
Thread.sleep(1000000);
//删除手表
boolean res = JNotify.removeWatch(watchID);
if(!res){
//指定无效的手表ID。
}
}
类Listener实现JNotifyListener {
public void fileRenamed(int wd,String rootPath,String oldName,
String newName ){
print(renamed+ rootPath +:+ oldName + - >+ newName);
}
public void fileModified(int wd,String rootPath,String name){
print(modified+ rootPath +:+ name);
}
public void fileDeleted(int wd,String rootPath,String name){
print(deleted+ rootPath +:+ name);
}
public void fileCreated(int wd,String rootPath,String name){
print(created+ rootPath +:+ name);
}
void print(String msg){
System.err.println(msg);
}
}
public static void main(String [] args)throws Exception {
new JNotifyDemo()。
}
}
输出: p>
修改C:\Documents and Settings\jigar:LOCALS〜1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C: \Documents and Settings\jigar:LOCALS〜1\Temp\etilqs_4s8ywsvyukghK0uDxRop
已修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\output1295531079119
已修改C: \Documents and Settings\jigar:Local Settings\Application Data\Google\Chrome\User Data\Default
deleted C:\Documents and Settings\jigar:Local Settings\Application Data \Google\Chrome\User Data\Default\Cache\f_001ea9
创建C:\Documents和Settings\jigar:Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\etilqs_04 gchL79ZJrpClZIqiom
修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\etilqs_04gchL79ZJrpClZIqiom
修改C:\Documents和Settings\jigar:本地设置\\应用数据\\ Google\Chrome\User Data\Default\Cache
已修改C:\Documents和Settings\jigar:Local Settings\Application Data\Google\Chrome\User Data\Default\\ \\ Cache\f_001eae
已修改C:\Documents和Settings\jigar:本地设置\应用数据\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\output1295531079119
修改C:\Documents和Settings\jigar:本地设置\应用数据\\\Chrome\\ \\用户数据\Default\Current会话
删除C:\Documents和Settings\jigar:本地设置\\应用数据\\ Google\Chrome\User Data\Default\Cache\f_001ea8
创建C:\Documents和Settings\jigar:Local Settings\Application Data\Google\Chrome\User Data\\ \\ Default\Cache\f_001eaf
已修改C:\Documents和Settings\jigar:本地设置\应用程序Data\Google\Chrome\User Data\Default\Cache
修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\etilqs_04gchL79ZJrpClZIqiom
修改C:\Documents和Settings\jigar:LOCALS〜1\Temp\etilqs_04gchL79ZJrpClZIqiom
修改C:\Documents和Settings\jigar:本地设置\应用程序Data\Google\Chrome\User Data\Default\Cache\f_001eaf
修改C:\Documents和Settings\\ \\ jigar:本地设置\应用数据\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\I have an application in which I want to listen to any changes made to a particular directory. The application should ping me as soon as there are any files added, deleted or updated in that directory.
解决方案 You can use JNotify
JNotify is a java library that allow java application to listen to file
system events, such as: File created
File modified File renamed File
deleted Supported platforms
Windows (2000 or newer) Windows notes
Linux with INofity support (2.6.14 or
newer) Linux notes Mac OS X (10.5 or
newer) Mac OS notes
More Info :
Download JNotify from here
Extract the zip, put .dll/.so according to platform in your lib path. and create a class provide jnotify-0.93.jar
in class path.
Sample code:
package org.life.java.stackoverflow.questions;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
/**
*
* @author Jigar
*/
public class JNotifyDemo {
public void sample() throws Exception {
// path to watch
String path = System.getProperty("user.home");
// 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());
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
Thread.sleep(1000000);
// to remove watch the watch
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// invalid watch ID specified.
}
}
class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print("renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
public static void main(String[] args) throws Exception {
new JNotifyDemo().sample();
}
}
Output:
modified C:\Documents and Settings\jigar: LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea9
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Current Session
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea8
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf
这篇关于Java中的目录侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!