其他分享
首页 > 其他分享> > 《文件与流》动手动脑

《文件与流》动手动脑

作者:互联网

1、使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件。

package text_001;

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.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
public class Action implements FileVisitor {
 private final PathMatcher matcher;
 private final long accepted_size;
 public Action(String glob,long accepted_size) {
      matcher= 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 = 1048576;//1M=1024k=1048576字节
    Path fileTree = Paths.get("C:/");
    Action walk=new Action(glob, size);
    EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    System.out.println("C盘中大小等于1M的文件有");
    Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
   }
}

 

 

标签:文件,java,nio,动脑,动手,IOException,file,import,size
来源: https://www.cnblogs.com/xhj1074376195/p/11829099.html