2019独角兽企业重金招聘Python工程师标准>>>
1.通过Apache的包进行文件监测
实例代码如下:
// 个人更推荐用Apache的包,这台机器没有Apache的lib,就做了一个JDK的。
import java.nio.file.FileSystems;
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;
public class FileMonitor {
public static void main(String[] args) throws Exception {
String filePath = "D:\\hlsXML\\";
WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get(filePath).register(watchService,
StandardWatchEventKinds.ENTRY_CREATE);
while (true) {
WatchKey key = watchService.take();
for (WatchEvent> event : key.pollEvents()) {
// System.out.println(event.context() + " " + event.kind());
String fileName = "D:\\hlsXML\\"+event.context();
//System.gc();
OutputXmlDemo.parserXml(fileName);
}
if (!key.reset()) {
break;
}
}
}
}
预览效果;