[File] JavaSE listener WatchService

First, the encounter scene

  There are two projects in APP, deployed on the same machine, A in the realization of a function requires B to do something that no other communication between the two in addition to APP.

I began to consider using a socket, but the socket that is too cumbersome and overkill. Finally decided to use document listeners, and because of this simple function provided by the operating system very reliable.

 

Second, on the WatchService.

  Its introduction I will not say more. It simply is a file listener, you can monitor file folder all file actions: create, modify, delete. This function is provided by the operating system. WatchService also just call the operating system resources.

 

Three, Sample

import java.io.IOException;
import java.nio.file.*;
import java.util.List;

public class Watcher implements Runnable{

    private String filePath;

    private WatchService watchService;

    public Watcher(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public void run() {
        try {
            this.watchService = FileSystems.getDefault().newWatchService();
            Path path = Paths.get(filePath);
            path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW);
            printLog("开始监听文件夹:{}", filePath);
            WatchKey key;
            while ((key=watchService.take()) != null) {
                List<WatchEvent<?>> watchEvents = key.pollEvents();
                for(WatchEvent<?> watchEvent : watchEvents) {
                    WatchEvent.Kind<?> kind = watchEvent.kind();
                    if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
                        printLog ( StandardWatchEventKinds.OVERFLOW) {"New File: {}, times: {}" , watchEvent.context (), watchEvent.count ()); 
                    } the else  IF (kind == StandardWatchEventKinds.ENTRY_DELETE) { 
                        printLog ( "Delete File: {}, times: { } " , watchEvent.context (), watchEvent.count ()); 
                    } the else  IF (kind == StandardWatchEventKinds.ENTRY_MODIFY) { 
                        printLog ( " modify file: {}, times: {} " , watchEvent.context (), watchEvent .count ()); 
                    } the else  IF (kind == 
                        printLog ( "overflow: {}, times: {}", watchEvent.context(), watchEvent.count());
                    }
                }
                key.reset();
            }

        } catch (IOException e) {
            System.out.println("获取监听器失败");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void printLog(String format, Object...objects) {
        format = format.replaceAll("\\{\\}", "%s");
        System.out.println(String.format(format,objects));
    }


    public static void main(String[] args) {
        new Thread(new Watcher("D:\\tmp")).start();
    }
}

 

Reproduced in: https: //www.cnblogs.com/yeyeck/p/11089536.html

Guess you like

Origin blog.csdn.net/weixin_33739523/article/details/94150797
Recommended