티스토리 뷰

public class Monitor {

private static Path sharedDirectoryPath;
private static WatchKey watchKey;
private static WatchService watchService;

public static void main(String[] args) throws Exception {
System.out.println("Start");
String path = "D:\\test";
String pathFile = "D:\\test\\test.txt";
sharedDirectoryPath = Paths.get(path);
try{
watchService = FileSystems.getDefault().newWatchService();
watchKey = sharedDirectoryPath.register(watchService
, StandardWatchEventKinds.ENTRY_CREATE
, StandardWatchEventKinds.ENTRY_DELETE
, StandardWatchEventKinds.ENTRY_MODIFY);

}catch(IOException e){
e.printStackTrace();
}
while(true){

 for(WatchEvent&lt?> watchEvent : watchKey.pollEvents()){ 
//System.out.println(watchEvent.context() + " " + watchEvent.kind());
if( watchEvent.kind() ==  StandardWatchEventKinds.ENTRY_CREATE ){
System.out.println("create");
try{
Thread.sleep(1);
File file = new File(pathFile);
FileReader filereader = new FileReader(file);
BufferedReader br = new BufferedReader(filereader);
String line = "";
while( (line = br.readLine()) != null ){
System.out.println(line);
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함