Java利用WatchService监听文件变化示例
发布时间:2020-08-20 05:36:21
来源:脚本之家
阅读:109
作者:自行车上的程序员
在实现配置中心的多种方案中,有基于JDK7+的WatchService方法,其在单机应用中还是挺有实践的意义的。
代码如下:
package com.longge.mytest;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
/**
* 测试JDK的WatchService监听文件变化
* @author yangzhilong
*
*/
public class TestWatchService {
public static void main(String[] args) throws IOException {
// 需要监听的文件目录(只能监听目录)
String path = "d:/test";