package cn.lg.file;
import java.io.File;
/**
* 批量修改文件
* Created by L on 2017/3/13.
*/
public class RenameFiles {
public static void main(String[] args) {
//文件夹目录
File fold = new File("/Users/L/Desktop/JAVA基础视频/day01/avi");
//列出文件夹从所有文件
File[] files = fold.listFiles();
for (File f:files) {
String oldName = f.getName();
//获得前缀
int index = oldName.indexOf("_");
String startName = oldName.substring(0,index+1);
//后缀名
int lastInt = oldName.lastIndexOf(".");
String endName = oldName.substring(lastInt,lastInt+4);
//中间名称
String midName;
int startMid = oldName.indexOf("(");
int endMId = oldName.indexOf(")");
if (startMid!=-1&&endMId!=-1){
midName = oldName.substring(startMid+1,endMId);
}else {
midName=oldName.substring(index+1,lastInt);
}
String newName = startName+midName+endName;
System.out.println(newName);
//重命名
File newFile = new File(fold,newName);
f.renameTo(newFile);
}
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。