java NIO文件系统监控

2021/4/26 1:26:32

本文主要是介绍java NIO文件系统监控,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package chpter15.test.ao1;

import java.io.IOException;
import java.nio.file.FileSystem;
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;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WatchServiceTest {

	public static void main(String[] args) throws IOException, InterruptedException {
		WatchService watchService = FileSystems.getDefault().newWatchService();
		Paths.get("c:\\").register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		while(true) {
			WatchKey key = watchService.take();
			for(WatchEvent event:key.pollEvents()) {
				System.out.println(bartDateFormat.format(new Date())+"\t"+"c:\\"+event.context()+"\t"+event.kind()+"\t");
				Thread.sleep(1);
			}
			
			boolean valid = key.reset();
			if(!valid) {
				break;
			}
		}
	}

}




这篇关于java NIO文件系统监控的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程