Java课堂动手动脑

时间:2025-02-01 22:33:50

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

代码:

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

运行结果:

Java课堂动手动脑

    2.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件。

package test;

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 WalkFileTree {
public static void main(String[] args) throws IOException {
String glob ="glob:**/*.{java,txt}";//采用组模式查找拓展名为.java和.txt的文件
String path ="E:/";//想要查询的目录
match(glob,path);
} public static void match(String glob,String location)throws IOException{
final PathMatcher pathmatcher =FileSystems.getDefault().getPathMatcher(glob);//新建pathmatcher对象用FileSystems.getDefault().getPathMatcher()方法接收匹配的字符串,返回一个可用的pathmatcher对象,然后进行匹配工作
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;
}});
}
}

运行结果:

Java课堂动手动脑