[JAVA] 디렉토리감시 이벤트

2024. 2. 17. 16:21카테고리 없음

디렉토리의 생성 및 파일의 변경이 일어날 경우 이를 감시하여 다른 서버로 ftp전송하거나, 다른 폴더에 복사할 일이 생겼다. 

주로 서비스의 변경 내용을 최신화 하기위한 방법으로 활용하고자 하여 내용을 정리해 본다. 

 

1. 해당 라이블러리 import 처리

해당라이블러리를 다운로드 하여 라이블러리를 추가한다. 

https://commons.apache.org/io/download_io.cgi

 

Commons IO – Download Apache Commons IO

Download Apache Commons IO Using a Mirror We recommend you use a mirror to download our release builds, but you must verify the integrity of the downloaded files using signatures downloaded from our main distribution directories. Recent releases (48 hours)

commons.apache.org

 

2. java 소스


import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;


public class FolderWatcherMain {
    public static void main(String ar[]) {
        try {
            MainStart();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void MainStart() throws Exception {
        String MonitorPath = "D:/PYTHON_WORKSTAGE/FOLDER";
        File rootDirectory = new File(MonitorPath);
        // 감시를 시작하는 메소드 호출
        startMonitoring(rootDirectory);


    }



    public static void startMonitoring(File directory) throws Exception {
        // FileAlterationObserver 생성
        FileAlterationObserver observer = new FileAlterationObserver(directory);

        // 감시할 디렉토리에 변경이 발생할 때 실행될 리스너 설정
        observer.addListener(new FileAlterationListener() {
            @Override
            public void onStart(FileAlterationObserver observer) {}

            @Override
            public void onDirectoryCreate(File directory) {
                System.out.println("디렉토리가 생성되었습니다: " + directory.getAbsolutePath());
                // 새로운 디렉토리에 대한 추가 작업 수행 가능
            }

            @Override
            public void onDirectoryChange(File directory) {}

            @Override
            public void onDirectoryDelete(File directory) {
                System.out.println("디렉토리가 삭제되었습니다: " + directory.getAbsolutePath());
                // 삭제된 디렉토리에 대한 추가 작업 수행 가능
            }

            @Override
            public void onFileCreate(File file) {
                System.out.println("파일이 생성되었습니다: " + file.getAbsolutePath());
                // 새로운 파일에 대한 추가 작업 수행 가능
            }

            @Override
            public void onFileChange(File file) {
                System.out.println("파일이 변경되었습니다: " + file.getAbsolutePath());
                // 변경된 파일에 대한 추가 작업 수행 가능
            }

            @Override
            public void onFileDelete(File file) {
                System.out.println("파일이 삭제되었습니다: " + file.getAbsolutePath());
                // 삭제된 파일에 대한 추가 작업 수행 가능
            }

            @Override
            public void onStop(FileAlterationObserver observer) {}
        });

        // FileAlterationMonitor 생성
        FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer); // 폴더를 감시할 간격 (밀리초)

        // 감시 시작
        monitor.start();
    }
}

 

해당 폴더의 하위에 파일을 생성하거나 삭제시 이벤트가 발생한다. 이벤트의 발생여부에 따라 파일을 처리하면 될 것 같다. 

 

반응형