以删除maven本地仓库中的“.lastUpdated”结尾的文件为例。
package com.demo.tools;
import java.io.File;
public class RemoveFiles {
public static void main(String[] args) {
String path = "D:\maven-repo\repo\"; //待处理的文件夹路径
dealFiles(path);
}
public static void dealFiles(String clientBase) {
File file = new File(clientBase);
if (file.isDirectory()) {
File[] files = file.listFiles();
for(File item: files) {
if (item.isDirectory()) {
//System.out.println("目录:" + item.getPath());
dealFiles(item.getPath()); //继续读取目录下文件或文件夹
} else {
//System.out.println("文件:" + item.getPath());
rmFile(item);
}
}
} else {
//System.out.println("文件:" + file.getPath());
rmFile(file);
}
}
public static void rmFile(File file) {
if(file.getName().endsWith(".lastUpdated")) {
file.delete();
System.out.println("删除文件:" + file.getPath());
} else if(file.getName().equals("_remote.repositories")) {
file.delete();
System.out.println("删除文件:" + file.getPath());
}
}
}
(学了那么的java,不能白费,将递归处理运用实际需要 —— 手动删除“.lastUpdated”结尾的文件太烦了)



