java實現文件重命名
背景
我們經常在網上下載一些視頻教程,然而這些視頻命名規(guī)則各不相同,即使對于相同類型的文件名來說,當文件數量很大且文件名全部是中文時,文件排序是非規(guī)則的,因此本篇博客主要講解一種改變文件夾名稱使得文件按照規(guī)律進行排序。
思路
根據文件名對文件進行排序,然后重命名文件即可。
代碼
規(guī)則:如 將文件名中帶有 “第八講 ”替換成“8”:
構建排序規(guī)則
/*** * @param filePath 文件夾位置 * @param startWorld 開始替換的字 * @param endWorld 結束替換的字*/ private void ReName(String filePath, String startWorld, String endWorld) { File file = new File(filePath); if (!file.exists() || !file.isDirectory()) { System.out.println('文件不存在'); return; } String[] list = file.list(); //以 第xxx講-文件全名 的鍵值對存儲文件 HashMap<String, String> paths = new HashMap<String, String>(); for (String str : list) { int start = str.indexOf(startWorld) + 1; int end = str.indexOf(endWorld); if (start != 0 && end != -1) {paths.put(str.substring(start, end), str); } else {System.out.println('文件 ' + str + ' 不滿足替換條件'); } } //對文件名進行排序 orderPath(filePath, endWorld, paths); }
排序
private void orderPath(String filePath, String endWorld, HashMap<String, String> paths) { if (paths.isEmpty()) { return; } TreeMap<Integer, String> map = new TreeMap<Integer, String>(); for (String str : paths.keySet()) { map.put(parseInt(str), paths.get(str)); } //重命名該文件 ReNameFile(filePath, endWorld, map); }
重命名
private void ReNameFile(String filePath, String endWorld, TreeMap<Integer, String> map) { for (int i : map.keySet()) { String path = map.get(i); File f = new File(filePath + File.separator + path); File dest = new File(filePath + File.separator + i + path.substring(path.indexOf(endWorld) + 1)); if (f.exists() && !dest.exists()) {f.renameTo(dest); } f = null; dest = null; } }
將中文描述的數字轉換為數字,如將 一百二十轉換為120
private int parseInt(String str) { if (str.length() == 1) { if (str.equals('十')) {return 10; } return getInt(str.charAt(0)); } else { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c != ’百’ && c != ’十’) { sb.append(getInt(c));} } int res = Integer.parseInt(sb.toString()); if (str.charAt(str.length() - 1) == ’百’) {res *= 100; } else if (str.charAt(str.length() - 1) == ’十’) {res *= 10; } if (str.charAt(0) == ’十’) {res += 10; } return res; } }
完整代碼
import java.io.File;import java.util.HashMap;import java.util.TreeMap;/** * 將一個文件夾中所有滿足條件的文件名替換 * <p> * 條件:將從開始字到結束字的字符串替換成對應的數字 * <p> * 如:第八講 替換成 8 */public class Main { public static void main(String[] args) { Main m = new Main(); // 文件夾位置 String filePath = 'D:新建文件夾OOAD與UML教學視頻'; // 從哪個字(startWorld)開始替換到哪個字(endWorld)結束 String startWorld = '第'; String endWorld = '講'; m.ReName(filePath, startWorld, endWorld); } /*** * @param filePath 文件夾位置 * @param startWorld 開始替換的字 * @param endWorld 結束替換的字 */ private void ReName(String filePath, String startWorld, String endWorld) { File file = new File(filePath); if (!file.exists() || !file.isDirectory()) { System.out.println('文件不存在'); return; } String[] list = file.list(); //以 第xxx講-文件全名 的鍵值對存儲文件 HashMap<String, String> paths = new HashMap<String, String>(); for (String str : list) { int start = str.indexOf(startWorld) + 1; int end = str.indexOf(endWorld); if (start != 0 && end != -1) {paths.put(str.substring(start, end), str); } else {System.out.println('文件 ' + str + ' 不滿足替換條件'); } } //對文件名進行排序 orderPath(filePath, endWorld, paths); } private void orderPath(String filePath, String endWorld, HashMap<String, String> paths) { if (paths.isEmpty()) { return; } TreeMap<Integer, String> map = new TreeMap<Integer, String>(); for (String str : paths.keySet()) { map.put(parseInt(str), paths.get(str)); } //重命名該文件 ReNameFile(filePath, endWorld, map); } private void ReNameFile(String filePath, String endWorld, TreeMap<Integer, String> map) { for (int i : map.keySet()) { String path = map.get(i); File f = new File(filePath + File.separator + path); File dest = new File(filePath + File.separator + i + path.substring(path.indexOf(endWorld) + 1)); if (f.exists() && !dest.exists()) {f.renameTo(dest); } f = null; dest = null; } } private int parseInt(String str) { if (str.length() == 1) { if (str.equals('十')) {return 10; } return getInt(str.charAt(0)); } else { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c != ’百’ && c != ’十’) { sb.append(getInt(c));} } int res = Integer.parseInt(sb.toString()); if (str.charAt(str.length() - 1) == ’百’) {res *= 100; } else if (str.charAt(str.length() - 1) == ’十’) {res *= 10; } if (str.charAt(0) == ’十’) {res += 10; } return res; } } private int getInt(char c) { int res = -1; switch (c) { case ’一’: res = 1; break; case ’二’: res = 2; break; case ’三’: res = 3; break; case ’四’: res = 4; break; case ’五’: res = 5; break; case ’六’: res = 6; break; case ’七’: res = 7; break; case ’八’: res = 8; break; case ’九’: res = 9; break; case ’零’: res = 0; break; } return res; }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: