[자바1.7,JAVA7]파일시스템의 변화감시,모니터링, 자바7, 자바NIO, java.nio
아래 예제는 자바7, JAVA1.7의 NIO를 이용하여 데몬처럼 떠있으면서 파일시스템의 파일의 변화 등을 감지할 수 있는 예제 입니다. 많은 활용 바랍니다.
by 오라클자바커뮤니티, 오엔제이실무프로그래밍교육센터
by 오라클자바커뮤니티, 오엔제이실무프로그래밍교육센터
import java.io.*;
import java.nio.file.*;
import java.nio.file.*;
public class FileChange
{
public static void main(String args[]) throws IOException, InterruptedException
{
monitoringDir("c:\\java");
}
static void monitoringDir(String dir) throws IOException, InterruptedException
{
WatchService myWatchService = FileSystems.getDefault().newWatchService();
//모니터링을 원하는 디렉토리 Path를 얻는다.
Path path = Paths.get(dir); // Get the directory to be monitored
//모니터링 서비스를 할 path에 의해 파일로케이션을 등록
WatchKey watchKey = path.register(myWatchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE); // Register the directory
//무한루프
while(true)
{
//변화가 감지되는 경우 이벤트 종류와 파일명을 출력
for (WatchEvent event : watchKey.pollEvents())
{
System.out.println(event.kind() + ": "+ event.context());
}
{
public static void main(String args[]) throws IOException, InterruptedException
{
monitoringDir("c:\\java");
}
static void monitoringDir(String dir) throws IOException, InterruptedException
{
WatchService myWatchService = FileSystems.getDefault().newWatchService();
//모니터링을 원하는 디렉토리 Path를 얻는다.
Path path = Paths.get(dir); // Get the directory to be monitored
//모니터링 서비스를 할 path에 의해 파일로케이션을 등록
WatchKey watchKey = path.register(myWatchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE); // Register the directory
//무한루프
while(true)
{
//변화가 감지되는 경우 이벤트 종류와 파일명을 출력
for (WatchEvent event : watchKey.pollEvents())
{
System.out.println(event.kind() + ": "+ event.context());
}
//* Resets this watch key.
//* @return {@code true} if the watch key is valid and has been reset
//* {@code false} if the watch key could not be reset because it is
//* no longer {@link #isValid valid}, 디렉토리등이 삭제되는 경우
if (!watchKey.reset())
{
break; // 디렉토리등이 삭제되는 경우
}
}
}
}
[결과 c:\java 폴더의 a.java를 b.java로 수정 후]
ENTRY_DELETE: a.java
ENTRY_CREATE: b.java
ENTRY_MODIFY: b.java
ENTRY_CREATE: b.java
ENTRY_MODIFY: b.java
오라클자바커뮤니티에서 설립한 개발자교육6년차 오엔제이프로그래밍 실무교육센터(오라클SQL,튜닝,힌트,자바프레임워크,안드로이드,아이폰,닷넷 실무개발강의)
[개강안내]오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주말]
[10/12]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[10/12]Spring3.X, MyBatis, Hibernate실무과정
[10/12]SQL초보에서실전전문가까지
[10/12]C#,ASP.NET마스터
[평일야간]
[10/10]SQL초보에서실전전문가까지
[10/15]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[10/17]Spring3.X, MyBatis, Hibernate실무과정
[평일주간]
[10/10]Spring3.X, MyBatis, Hibernate실무과정
[10/17]PL/SQL,오라클힌트,SQL튜닝,사례연구
[주말]
[10/12]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[10/12]Spring3.X, MyBatis, Hibernate실무과정
[10/12]SQL초보에서실전전문가까지
[10/12]C#,ASP.NET마스터
[평일야간]
[10/10]SQL초보에서실전전문가까지
[10/15]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[10/17]Spring3.X, MyBatis, Hibernate실무과정
[평일주간]
[10/10]Spring3.X, MyBatis, Hibernate실무과정
[10/17]PL/SQL,오라클힌트,SQL튜닝,사례연구
댓글 없음:
댓글 쓰기