Java文件与流-文件与流-NIO

涎涎原创约 276 字小于 1 分钟...JavaJava

233-Java文件与流-文件与流-NIO.mdopen in new window

注意

本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。

概念

示例代码

package com.tencent.chapter03.file;

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;

public class WatchServiceDemo {

	public static void main(String[] args) {
		/*监控目标路径 */
		Path path = Paths.get("D:\\temp");
		try {
			/* 创建文件监控对象 */
			WatchService watchService = FileSystems.getDefault().newWatchService();
			/* 注册文件监控的所有事件类型 */
			path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
					StandardWatchEventKinds.ENTRY_DELETE,
					StandardWatchEventKinds.ENTRY_MODIFY);
			/* 循环监测文件 */
			while (true) {
				WatchKey watchKey = watchService.take();
				/* 迭代触发事件的所有文件 */
				for (WatchEvent<?> event : watchKey.pollEvents())
					System.out.println(event.context().toString() + " 事件类型:"
							+ event.kind());
				if (!watchKey.reset())
					return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
}

分割线


相关信息

以上就是我关于 Java文件与流-文件与流-NIO 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。

上次编辑于:
贡献者: 涎涎
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.4