java實(shí)現(xiàn)文件重命名
背景
我們經(jīng)常在網(wǎng)上下載一些視頻教程,然而這些視頻命名規(guī)則各不相同,即使對(duì)于相同類型的文件名來(lái)說(shuō),當(dāng)文件數(shù)量很大且文件名全部是中文時(shí),文件排序是非規(guī)則的,因此本篇博客主要講解一種改變文件夾名稱使得文件按照規(guī)律進(jìn)行排序。
思路
根據(jù)文件名對(duì)文件進(jìn)行排序,然后重命名文件即可。
代碼
規(guī)則:如 將文件名中帶有 “第八講 ”替換成“8”:
構(gòu)建排序規(guī)則
/*** * @param filePath 文件夾位置 * @param startWorld 開(kāi)始替換的字 * @param endWorld 結(jié)束替換的字*/ 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講-文件全名 的鍵值對(duì)存儲(chǔ)文件 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 + ' 不滿足替換條件'); } } //對(duì)文件名進(jìn)行排序 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; } }
將中文描述的數(shù)字轉(zhuǎn)換為數(shù)字,如將 一百二十轉(zhuǎn)換為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;/** * 將一個(gè)文件夾中所有滿足條件的文件名替換 * <p> * 條件:將從開(kāi)始字到結(jié)束字的字符串替換成對(duì)應(yīng)的數(shù)字 * <p> * 如:第八講 替換成 8 */public class Main { public static void main(String[] args) { Main m = new Main(); // 文件夾位置 String filePath = 'D:新建文件夾OOAD與UML教學(xué)視頻'; // 從哪個(gè)字(startWorld)開(kāi)始替換到哪個(gè)字(endWorld)結(jié)束 String startWorld = '第'; String endWorld = '講'; m.ReName(filePath, startWorld, endWorld); } /*** * @param filePath 文件夾位置 * @param startWorld 開(kāi)始替換的字 * @param endWorld 結(jié)束替換的字 */ 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講-文件全名 的鍵值對(duì)存儲(chǔ)文件 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 + ' 不滿足替換條件'); } } //對(duì)文件名進(jìn)行排序 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; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)2. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 如何在jsp界面中插入圖片4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. phpstudy apache開(kāi)啟ssi使用詳解6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令10. xml中的空格之完全解說(shuō)
