JNotify를 사용하십시오. 빌드 경로에 jnotify.jar을 추가하고 jnotify.dll jnotify_64bit.dll과 jdk의 lib 안에 두 개의 dll 파일을 넣기 만하면됩니다. 데모 프로그램은
package jnotify;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
public class MyJNotify {
public void sample() throws Exception {
String path = "Any Folder location here which you want to monitor";
System.out.println(path);
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) {
System.out.println("Invalid");
}
}
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) {
try {
new MyJNotify().sample();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
IIRC, JNotify는 OS 특정 DLL이 필요합니다. 실행 가능성이 높지는 않지만 (예, 실제 단어는 아님) JPoller는 순수 Java 구현을 제공합니다. –
Jpoller가 미친 듯이 여론 조사를한다고 생각하기 때문에 Java7을 사용하기 전까지는 항상 약간의 해킹이 발생합니다. JNotify는 네이티브가 아닙니다. – Alfred
JNotify가 원주민이 아닌 것을 의미하는 것이 확실하지 않습니다. OS 별 DLL/라이브러리가 필요합니다. –