批量修改文件名
package com.zdxf.common;
/**
* @Author: zj
* @Date: 2023/3/21 0021 18:17
* @Description:
*/
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SaveFileUtil {
/**
* 保存文件
* @param stream 文件流
* @param path 保存文件夹地址
* @param fileName 保存文件名
* @throws IOException 异常
*/
public static void saveFile(InputStream stream, String path, String fileName) throws IOException {
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream( path + "/"+ fileName));
byte[] bs = new byte[1024];
int len;
while ((len = stream.read(bs)) != -1){
bos.write(bs,0,len);
bos.flush();
}
bos.close();
stream.close();
}
/**
* 生成一个新的文件名(没有后缀名)
*
* @return 文件名
*/
public static String createFileName() {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return fmt.format(new Date());
}
/**
* 替换文件名字
* @param file
* @param beforeStr
* @param afterStr
*/
private static void replaceFilesName(File file, String beforeStr, String afterStr) {
//如果不是目录,直接修改文件
if (!file.isDirectory()) {
updateFileName(file, beforeStr, afterStr);
return;
}
//已经是目录
//如果目录中含有关键字beforeStr;创建新的目录,否则,使用以前的目录
if (updateFileName(file, beforeStr, afterStr)) {
file = new File(file.getParent()+ File.separator + file.getName().replace(beforeStr, afterStr));
}
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
//如果是目录,则递归
replaceFilesName(f, beforeStr, afterStr);
}else {
//如果是文件,则修改文件名
updateFileName(f, beforeStr, afterStr);
}
}
}
/**
* 修改单个文件的名字
* @param file
* @param beforeStr
* @param afterStr
*/
private static boolean updateFileName(File file, String beforeStr, String afterStr) {
String fileName = file.getName();
System.out.println(fileName);
if(fileName.equals("【更多精品资源点这里免费领取!】.txt") || fileName.equals("【如果你看到的文件夹是空的请点这里查看完整版(原始链接)】.txt")) {
file.delete();
System.out.println("删除文件:" + fileName);
return true;
}
if (fileName.contains(beforeStr)) {
fileName = fileName.replace(beforeStr, afterStr);
file.renameTo(new File(file.getParent()+File.separator+fileName));
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
File file = new File("D:\\知识");
replaceFilesName(file, "【公益知识库zscc.club】","");
// SaveFileUtil.copyFile("F:\\12.txt", "F:\\op.txt");
// SaveFileUtil.printWrite("F:\\12.txt");
// SaveFileUtil.copyWrite("F:\\12.txt", "F:\\111.txt");
// SaveFileUtil.copyDirs("", "");
}
/**
* 拷贝文件:从一个盘到另一个盘
* @param beforeDir
* @param afterDir
*/
private static void copyDirs(String beforeDir, String afterDir) throws FileNotFoundException {
FileInputStream in = new FileInputStream(beforeDir);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
}
private static void copyWrite(String beforeFile, String afterFile) throws IOException {
FileReader reader = new FileReader(beforeFile);
FileWriter writer = new FileWriter(afterFile);
//cope1(reader, writer);
cope2(reader, writer);
reader.close();
writer.flush();
writer.close();
}
private static void cope2(FileReader reader, FileWriter writer) throws IOException {
int len = 0;
char[] chars = new char[1024];
while ((len = reader.read(chars)) != -1) {
writer.write(chars, 0, len);
}
}
private static void cope1(FileReader reader, FileWriter writer) throws IOException {
int len;
while ((len=reader.read()) != -1) {
writer.write(len);
}
}
private static void printWrite(String filePath) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("F:\\12.txt");
// 定义变量,保存数据
int b;
// 循环读取
while ((b = fr.read())!=-1) {
System.out.println((char)b);
}
// 关闭资源
fr.close();
}
private static void copyFile(String beforeFile, String afterFile) throws IOException {
FileInputStream input = null;
FileOutputStream out = null;
try {
input = new FileInputStream(beforeFile);
out = new FileOutputStream(afterFile);
int len;
byte[] b = new byte[1024];
while ( (len = input.read(b)) != -1 ) {
out.write(b, 0, len);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(out);
}
}
}