1. 程式人生 > >Java檔名==>修改器

Java檔名==>修改器

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, "");
		}
	}
}