九、nio.file
该包是1.7新出的,包含了一系列高级的文件和目录操作方法
1、控制目录属性,只读,系统之类的
2、监控文件及文件夹的改变的WatchService
public void startWatch() throws IOException, InterruptedException { final FileSystem fileSystem = FileSystems.getDefault(); try (final WatchService watchService = fileSystem.newWatchService()) { final Map<WatchKey, Path> keyMap = new HashMap<WatchKey, Path>(); final Path path = Paths.get(watchFolder); keyMap.put(path.register(watchService, ENTRY_CREATE),path); WatchKey watchKey; do { watchKey = watchService.take(); final Path eventDir = keyMap.get(watchKey); for (final WatchEvent<?> event : watchKey.pollEvents()) { final Kind<?> kind = event.kind(); final Path eventPath = (Path) event.context(); System.out.println(event.kind() + ": " + eventDir + "\\" + event.context()); Thread.sleep(500); if(kind == ENTRY_CREATE) //创建文件且后缀不是.uploaded this.put(eventPath.toString()); } } while (watchKey.reset()); } }
及其他
时间: 2024-12-16 03:48:38