package com.toec;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
public class TestFilesWalkFileTree {
public static void main(String[] args) throws IOException {
// fileAndDirCount();
// jarCount();
Files.walkFileTree(Paths.get("G:\\tools\\02\\download"),new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("============>进入:"+dir);
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("==========="+file);
// 删除文件,不经过回收站,谨慎操作
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
// 删除文件夹,不经过回收站,谨慎操作
Files.delete(dir);
System.out.println("<============退出:"+dir);
return super.postVisitDirectory(dir, exc);
}
});
}
//计算文件数量
public static void jarCount() throws IOException {
Path path = Paths.get("E:\\ProgramDatas\\java\\jdk");
AtomicInteger jarCount = new AtomicInteger();
Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(file.toString().endsWith(".jar")){
System.out.println(file);
jarCount.incrementAndGet();
}
return super.visitFile(file, attrs);
}
});
System.out.println("============>"+jarCount);
}
//遍历文件夹
public static void fileAndDirCount() throws IOException {
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
Path path = Paths.get("E:\\ProgramDatas\\java\\jdk");
Files.walkFileTree(path,new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("===============>"+dir);
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("===========>"+file);
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return super.postVisitDirectory(dir, exc);
}
});
System.out.println("dir Count"+dirCount);
System.out.println("file Count"+fileCount);
}
}
文件目录拷贝
package com.toec;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class TestFilesCopy {
public static void main(String[] args) throws IOException {
String source = "G:\\tools\\4-19-2022\\2_level\\down_package";
String target = "G:\\tools\\4-19-2022\\2_level\\copy_down";
Files.walk(Paths.get(source)).forEach(path -> {
try {
String targetName = path.toString().replace(source, target);
// 目录处理
if(Files.isDirectory(path)){
Files.createDirectories(Paths.get(targetName));
}else if(Files.isRegularFile(path)){
Files.copy(path,Paths.get(targetName));
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
版权声明:本文为weixin_45717907原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。