1、代码:
1 import java.io.IOException; 2 import java.nio.file.FileSystems; 3 import java.nio.file.FileVisitResult; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.PathMatcher; 7 import java.nio.file.Paths; 8 import java.nio.file.SimpleFileVisitor; 9 import java.nio.file.attribute.BasicFileAttributes; 10 11 public class Action { 12 public static void main(String args[]) throws IOException { 13 String glob = "glob:**/*.{java,txt}"; 14 String path = "D:\Clark\2020下半年\软工\06.PolymorphismSourceCode"; 15 match(glob, path); 16 } 17 public static void match(String glob, String location) throws IOException { 18 final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); 19 Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { 20 @Override 21 public FileVisitResult visitFile(Path path, 22 BasicFileAttributes attrs) throws IOException { 23 if (pathMatcher.matches(path)) { 24 System.out.println(path); 25 } 26 return FileVisitResult.CONTINUE; 27 } 28 @Override 29 public FileVisitResult visitFileFailed(Path file, IOException exc) 30 throws IOException { 31 return FileVisitResult.CONTINUE; 32 } 33 }); 34 } 35 }
运行结果:
2、代码:
1 import java.io.IOException; 2 import java.nio.file.FileSystems; 3 import java.nio.file.FileVisitOption; 4 import java.nio.file.FileVisitResult; 5 import java.nio.file.FileVisitor; 6 import java.nio.file.Files; 7 import java.nio.file.Path; 8 import java.nio.file.PathMatcher; 9 import java.nio.file.Paths; 10 import java.nio.file.attribute.BasicFileAttributes; 11 import java.util.EnumSet; 12 public class Action2 implements FileVisitor { 13 private final PathMatcher matcher; 14 private final long accepted_size; 15 public Action2(String glob,long accepted_size) { 16 matcher= FileSystems.getDefault().getPathMatcher("glob:" +glob); 17 this.accepted_size=accepted_size; 18 } 19 void search(Path file) throws IOException { 20 long size = (Long) Files.getAttribute(file, "basic:size"); 21 if(size ==accepted_size) { 22 System.out.println(file); 23 } 24 } 25 @Override 26 public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException { 27 return FileVisitResult.CONTINUE; 28 } 29 @Override 30 public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException { 31 return FileVisitResult.CONTINUE; 32 } 33 @Override 34 public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException { 35 search((Path) file); 36 return FileVisitResult.CONTINUE; 37 } 38 @Override 39 public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException { 40 return FileVisitResult.CONTINUE; 41 } 42 public static void main(String[] args) throws IOException{ 43 String glob= "*.jpg"; 44 long size = 1048576;//1M=1024k=1048576字节 45 Path fileTree = Paths.get("C:/"); 46 Action2 walk=new Action2(glob, size); 47 EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS); 48 System.out.println("C盘中大小等于1M的文件有"); 49 Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk); 50 } 51 }
运行结果:
3、代码:
1 import java.nio.file.FileSystems; 2 import java.nio.file.Path; 3 import java.nio.file.Paths; 4 import java.nio.file.StandardWatchEventKinds; 5 import java.nio.file.WatchEvent; 6 import java.nio.file.WatchKey; 7 import java.nio.file.WatchService; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 public class FileWatcherDemo { 12 /** 13 * @param args 14 */ 15 public static void main(String[] args) { 16 //创建一个WatchService对象,此对象将用于管理特定文件夹的变动信息队列。 17 try(WatchService service=FileSystems.getDefault().newWatchService()) { 18 //此集合可保存多个文件夹的监控信息,当前只使用了一个 19 Map<WatchKey, Path> keyMap=new HashMap<>(); 20 Path path=Paths.get("D:\test"); 21 //设置WatchService对象管理的内部队列,将保存指定的文件夹的创建、删除和修改事件信息 22 //返回的WatchKey对象可用于从事件队列中获取事件对象 23 WatchKey key=path.register(service, StandardWatchEventKinds.ENTRY_CREATE, 24 StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY); 25 keyMap.put(key, path); 26 do { 27 //开始监控,阻塞等待,当感兴趣的事件发生时,take()方法返回。 28 key=service.take(); 29 Path eventDir=keyMap.get(key); 30 //从事件队列中提取所有的事件 31 for (WatchEvent<?> event : key.pollEvents()) { 32 //是什么类型的事件? 33 WatchEvent.Kind<?> kind=event.kind(); 34 //是哪个对象发生了变动? 35 Path eventPath=(Path)event.context(); 36 System.out.println(eventDir+":"+kind+":"+eventPath); 37 } 38 } while (key.reset()); //reset方法重置此对象,让其可以重新接收信息 39 } catch (Exception e) { 40 } 41 } 42 }
运行结果:
4、JDK7中定义了一个WatchService接口,可用于监控特定对象的“动静”。 WatchService内部管理着被监控对象的一个“队列”,用于存储此对象变动的信息。 被监控的对象必须实现Watchable接口(Path对象就实现了Watch接口),它的register方法将它与WatchService相关联,此方法返回一个实现了WatchKey接口的对象,此对象的pollEvents()方法可用于获取对象变动的信息列表(包容一个或多个WatchEvent对象)。 对象变动的信息由WatchEvent对象代表,包容变动的种类和激发次数两个重要信息。