动手动脑:文件与流
1.使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.test; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitOption; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.EnumSet; public class File implements FileVisitor<Object> { private final long accepted_size; public File(String glob, long accepted_size) { FileSystems.getDefault().getPathMatcher( "glob:" +glob); this .accepted_size=accepted_size; } void search(Path file) throws IOException { long size = (Long) Files.getAttribute(file, "basic:size" ); if (size <=accepted_size) { System.out.println(file); } } @Override public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { search((Path) file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } public static void main(String[] args) throws IOException{ String glob= "*.jpg" ; long size = 1024 ; Path fileTree = Paths.get( "D:/" ); File walk= new File(glob, size); EnumSet<FileVisitOption> opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS); System.out.println( "D盘中大于等于1KB的文件有" ); Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk); } } |
2.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class FindTxtJava { public static void main(String args[]) throws IOException { String glob = "glob:**/*.{java,txt}" ; String path = "D:/" ; match(glob, path); } public static void match(String glob, String location) throws IOException { final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path)) { System.out.println(path); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } } |
3.使用Files. walkFileTree()找出指定文件夹下所有包容指定字符串的txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import java.io.IOException; import java.io.*; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class FileGlobNIO { public static void main(String args[]) throws IOException { String glob = "glob:**/*.txt" ; String path = "D:\\wenjian" ; match(glob, path); } public static void match(String glob, String location) throws IOException { final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path)) { BufferedReader reader =Files.newBufferedReader(path); //读取文件内的内容 String line= null ; while ((line = reader.readLine())!= null ) { if (line.equals( "account" )) //若读取的内容等于“account"则输出文件名 { System.out.println(path); break ; } } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); } } |
动手动脑二:
1.请通过查询JDK文件和使用搜索引擎等方式,看懂此示例代码,并弄明白Watchable、WatchService等类型之间的关系,使用UML类图表示出这些类之间的关系?
WatchService
看作是文件监控器,通过操作系统原生文件系统来运行。
针对单点多appkey的情况,可以注册开启多个监控器。
每个监控器可看作是后台线程,通过监控文件发出的信号来实现监控。
WatchService 用来观察被注册了的对象所有的变化和事件
Watchable 被观察者,与WatchService结合使用, java.nio.file.Path 已经实现
WatchService 实例化:
WatchService watchService = FileSystems.getDefault().newWatchService();
利用 Path 实例化监控对象 Watchable
Path dir = Paths.get(path);
将 Path 注册到 WatchService 中//这里监控文件的 创建、修改、删除 但是这里返回的key里面的监控信息为空
WatchKey key = dir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
注意:监控池是静态的,只有当你主动去获取新的监控池时才会有更新的内容加入监控池。这就造成了系统接收到监控信息事件可能稍长的问题。
1,java.nio.file.WatchService文件系统监视服务的接口类,它的具体实现由监视服务提供者负责加载。
2,ava.nio.file.Watchable 实现了 java.nio.file.Watchable 的对象才能注册监视服务 WatchService。java.nio.file.Path实现了 watchable 接口,后文使用 Path 对象注册监视服务。
3,java.nio.file.WatchKey 该类代表着 Watchable 对象和监视服务 WatchService 的注册关系。WatchKey 在 Watchable 对象向 WatchService 注册的时候被创建。它是 Watchable 和 WatchService 之间的关联类。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构