java批量修改文件名
下载了一个系类的优酷《原创精选》发现文件名很恶心,带有各种不要的字段,使文件名变得很长,看不完全
最关键的是:由于顺序强迫症,喜欢按照顺序观看,前面是中文,无法按照文件名排序,中文的后面才是更新日期
我要的格式:130601_智商硬伤混黑道笑惨了.mp4
这样可以按照文件名进行排序,就可以从头开始观看了
于是萌生出写一个工具类来批量重命名一下
于是有了下面的一个工具类RenameFolderUtils.java:
package com.dhh.Utils;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* 批量修改文件名工具类
* 需求:
* 当下载很多视频后,视频文件名中总会有很多的不要的字段,比如有文件名中有电影的网址等,高清字段等,
* 这些有碍于清楚的知道文件内容
* 关键是当批量下载很多集视频后,它的播出时间会写在题目的后面140531表示2014.5.31号更新的视频,
* 如果能将这些放在前面去,视频将变得有顺序,就可以根据日期排序
* 作用:
* 将更新日期提前,去掉文件名中碍眼的字段
* @author dhh
*
*/
public class RenameFolderUtils {
public String srcPath;
public static void main(String[] args) {
//可以根据自己的需求来修改
String srcPath = "F:\\Youku Files\\download"; //原路径
String destPath = null; //目标路径
Map<String, String> condition = new HashMap<String, String>();
//组拼条件
condition.put("_",""); //去掉下划线
condition.put("-", ""); //去掉中划线
condition.put("高清", ""); //去掉"_高清"
condition.put(" ", ""); //去掉空格
String decoration = "_"; //数字和字符以下划线相隔
boolean isFirstDecided = false; //位数相同以后面为准
boolean isDeep = true; //是否深度扫描
renameFolder(srcPath, destPath ,condition,
decoration, isFirstDecided, isDeep);
}
/**
* 批量修改文件名的主要工具类
* 注意:路径名最后一律不带\\,程序会自动添加上去
* @param srcPath
* 原路径(要重命名的文件夹路径),如果是具体文件路径,则只修改这个文件,字段不能为NULL或者"",
* @param destPath
* 目标路径(修改后存在的路径)如果为NULL或者""值,则代表修改后存放在原来的路径
* @param condition
* 条件值HashMap<String,String>,HashMap第一个表示文件名中不想要的名字,
* 第二个值表示要替换的值,如果只想把第一个值删除,那么第二个参数填"",采用的是replace方法
* @param decoration
* 修饰附,数字与字符之间的相隔符号,一般用下划线_
* @param isFirstDecided
* 算法要实现将原来文件名的数字提到前面去,首先按照位数来判定,位数长的放在最前面,当位数相同时要采取哪种策略,
* 由该参数来决定,true表示当位数相同时,由前面的数字决定,false表示由后面数字决定
* eg.java视频140523传智播客140623.avi 这个文件名如果填true,则为
* 140523java视频传智播客140623.avi,否则为:140623java视频140523传智播客140623.avi
* @param isDeap
* 是否要深度扫描,即表示文件夹中的文件夹是否也要修改,true表示深度扫描,
* false表示只扫描原路径中所有文件,原路径中的文件夹不管
*/
public static void renameFolder(String srcPath,String destPath,
Map<String, String> condition,String decoration,
boolean isFirstDecided,boolean isDeap){
try {
File file = new File(srcPath);
if(file.exists() && file.isDirectory()){
File[] files = file.listFiles();
if(files.length>0){ //里面有文件
System.out.println("大小为:"+files.length);
for(File myfile : files){
String fileAllName = myfile.getName(); //包括后缀
if(myfile.isFile()){
//得到所有的文件名,不包括后缀
String fileName = fileAllName.substring(0,fileAllName.lastIndexOf("."));
//得到文件的后缀名
String suffix = fileAllName.substring(fileAllName.lastIndexOf("."),
fileAllName.length());
//修改文件名
String newName = "";
newName = replaceWord(fileName, condition); //先替换不要的词
newName = makeNumFirst(newName,decoration, isFirstDecided);//替换位置
String realName = "" ; //新文件的全路径名
if(destPath!=null && !destPath.equals(""))
{
File destFile = new File(destPath);
if(!destFile.exists()) //如果目标文件夹不存在的话
destFile.mkdirs(); //创建这个文件夹
if(destFile.exists()&&destFile.isFile()){
System.out.println("目标文件夹不能为一个文件,请重新输入!");
return;
}
realName = destPath + "\\" + newName+suffix ;
}else{
realName = srcPath + "\\" + newName+suffix;
}
System.out.println("原来名字为:"+fileAllName);
System.out.println("新名为:"+newName+suffix);
System.out.println("==================================");
//设置新的文件名
myfile.renameTo(new File(realName));
System.out.println("批量重命名成功!");
}else{
//递归调用,子文件夹中的文件也重命名
if(isDeap){
renameFolder(myfile.getAbsolutePath(),destPath ,condition,
decoration, isFirstDecided,isDeap);
}
}
}
}else{
System.out.println("文件夹为空!");
}
}else if(file.exists() && file.isFile()){ //要修改的只是一个文件,而不是文件夹
String fileAllName = file.getName(); //包括后缀
//得到所有的文件名,不包括后缀
String fileName = fileAllName.substring(0,fileAllName.lastIndexOf("."));
//得到文件的后缀名
String suffix = fileAllName.substring(fileAllName.lastIndexOf("."),
fileAllName.length());
//修改文件名
String newName = "";
newName = replaceWord(fileName, condition); //先替换不要的词
newName = makeNumFirst(newName,decoration, isFirstDecided);//替换位置
String realName = "" ; //新文件的全路径名
if(destPath!=null && !destPath.equals(""))
{
File destFile = new File(destPath);
if(!destFile.exists()) //如果目标文件夹不存在的话
destFile.mkdirs(); //创建这个文件夹
if(destFile.exists()&&destFile.isFile()){
System.out.println("目标文件夹不能为一个文件,请重新输入!");
return;
}
realName = destPath + "\\" + newName+suffix ;
}else{
realName = srcPath + "\\" + newName+suffix;
}
System.out.println("原来名字为:"+fileAllName);
System.out.println("新名为:"+newName+suffix);
System.out.println("==================================");
//设置新的文件名
file.renameTo(new File(realName));
System.out.println("批量重命名成功!");
}else{
System.out.println("原路径名不存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 按照Map中的条件替换word
* @param word
* 要替换的字符串
* @param condition
* 条件值HashMap<String,String>,HashMap第一个表示文件名中不想要的名字,
* 第二个值表示要替换的值,如果只想把第一个值删除,
* 那么第二个参数填"",采用的是replace方法
* @return
* 替换后的名字
*/
public static String replaceWord(String word,Map<String, String> condition){
String newWord = "";
if(condition.size()>0){
for(Entry<String, String> entry : condition.entrySet()){
newWord = word.replace(entry.getKey(), entry.getValue());
word = newWord;
}
}
return newWord;
}
/**
* 将数字放在前面
* @param word
* 要处理的字符串
* @param decoration
* 数字放在第一后,数字与后面字符之间的间隔符号,一般使用下划线_
* @param isFirstDecided
* 算法要实现将原来文件名的数字提到前面去,首先按照位数来判定,位数长的放在最前面,当位数相同时要采取哪种策略,
* 由该参数来决定,true表示当位数相同时,由前面的数字决定,false表示由后面数字决定
* @return
* 处理后的字符串
*/
public static String makeNumFirst(String word,String decoration,
boolean isFirstDecided){
char[] charWords = word.toCharArray();
StringBuffer strBuff = new StringBuffer(); //用来拼加字符
String tempMax = new String(); //用来选择出较大的连接数字
for(int i = 0 ;i<charWords.length; i++){
char charword = charWords[i];
if((int)charword>=48&&(int)charword<=57){ //选出数字
strBuff.append(charword); //拼加数字
if(i==charWords.length-1){ //如果是最后一个,也要比较
if(isFirstDecided){
if(strBuff.length()> tempMax.length()){
tempMax = strBuff.toString();
}
}else{
if(strBuff.length()>= tempMax.length()){
tempMax = strBuff.toString();
}
}
}
}else{
if(isFirstDecided){
if(strBuff.length()> tempMax.length()){
tempMax = strBuff.toString();
}
}else{
if(strBuff.length()>= tempMax.length()){
tempMax = strBuff.toString();
}
}
strBuff.delete(0, strBuff.length()); //清空strBuff
}
}
if(tempMax.length()==0){ //如果没有数字
return word;
}else{
return tempMax + decoration + word.replace(tempMax, "");
}
}
}