package com.javaee.demo;
import java.util.*;
import java.io.*;
public class FileDemo {
public static void main(String[] args) {
String path = "E:\\学习\\尚硅谷-全栈在线教育项目-谷粒学院【Vue.js+Spring Cloud Alibaba】\\课件\\谷粒学院笔记";
File file = new File(path);
ForFile(file);
}
// 批量修改文件
public static void batchUpdate(String pathname) {
File file = new File(pathname);
String[] flist = file.list();
Arrays.asList(flist).stream().forEach(x -> {
if (x.contains("ziw")) {
String cc = x.replace("ziw", "zip");
File old = new File(pathname + File.separatorChar + x);
File nfile = new File(pathname + File.separatorChar + cc);
old.renameTo(nfile);
}
});
Arrays.asList(flist).stream().forEach(x -> System.out.println(x));
}
// 遍历文件夹并修改文件
public static void ForFile(File file) {
File[] list = file.listFiles();
for (File file2 : list) {
if (file2.isDirectory()) {
System.out.println("目录:" + file2);
String pathname = file2.toString();
batchUpdate(pathname);
ForFile(file2);
} else {
System.out.print("文件:" + file2);
}
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4063061/blog/4322136