자바(Java)
자바 폴더 실시간 모니터링 중 파일처리
xemaker
2019. 7. 19. 09:28
이전 글에서 폴더 모니터링을 봤으니 이번글에서는 파일을 받아 처리하는 부분을 살펴보자.
public class Monitor{
private static Path sharedDirectoryPath;
private static WatchKey watchKey;
private static WatchService watchService;
public static void main(String[] args){
String path="D:\\test";
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<?> 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();
}
}
}
}
public class Monitor{
private static Path sharedDirectoryPath;
private static WatchKey watchKey;
private static WatchService watchService;
public static void main(String[] args){
String path="D:\\test";
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<?> 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();
}
}
}
}