java实现文件重命名
作者:zjf1165 时间:2023-08-26 09:27:46
背景
我们经常在网上下载一些视频教程,然而这些视频命名规则各不相同,即使对于相同类型的文件名来说,当文件数量很大且文件名全部是中文时,文件排序是非规则的,因此本篇博客主要讲解一种改变文件夹名称使得文件按照规律进行排序。
思路
根据文件名对文件进行排序,然后重命名文件即可。
代码
规则:如 将文件名中带有 “第八讲 ”替换成“8”:
构建排序规则
/***
* @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;
}
}
来源:https://blog.csdn.net/zjf1165/article/details/52473624
标签:java,文件,重命名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring框架中一个有用的小组件之Spring Retry组件详解
2021-05-27 10:07:40
Android拼图游戏 玩转从基础到应用手势变化
2021-08-24 02:41:59
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
java设计模式笔记之适配器模式
2021-12-27 05:27:30
![](https://img.aspxhome.com/file/2023/2/62242_0s.png)
C#利用iTextSharp组件给PDF文档添加图片/文字水印
2021-11-03 20:18:31
java基础的详细了解第八天
2023-11-08 10:47:47
![](https://img.aspxhome.com/file/2023/7/65347_0s.jpg)
java实现列表、集合与数组之间转化的方法
2023-11-29 10:17:38
关于Java中配置ElasticSearch集群环境账号密码的问题
2022-10-20 09:10:18
![](https://img.aspxhome.com/file/2023/9/94689_0s.png)
Java实现多人聊天室(含界面)
2021-07-15 23:18:36
![](https://img.aspxhome.com/file/2023/3/92563_0s.jpg)
C#中==(双等于号)与equals()区别详解
2021-09-21 18:49:42
![](https://img.aspxhome.com/file/2023/0/70440_0s.png)
spring boot使用thymeleaf为模板的基本步骤介绍
2023-12-13 15:07:23
![](https://img.aspxhome.com/file/2023/9/98699_0s.png)
Android Build Variants 为项目设置变种版本的方法
2023-04-26 10:29:07
![](https://img.aspxhome.com/file/2023/1/89541_0s.jpg)
Android学习教程之日历控件使用(7)
2023-01-06 23:10:46
![](https://img.aspxhome.com/file/2023/6/123326_0s.jpg)
C#byte数组与Image的相互转换实例代码
2023-08-15 16:15:51
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
![](https://img.aspxhome.com/file/2023/9/82419_0s.gif)
Springboot自动装配实现过程代码实例
2023-11-14 19:50:19
![](https://img.aspxhome.com/file/2023/3/69303_0s.png)
C#使用DoddleReport快速生成报表
2022-05-31 06:39:59
![](https://img.aspxhome.com/file/2023/2/84422_0s.png)
Java 使用Docker时经常遇到的五个问题
2023-12-10 07:20:21
![](https://img.aspxhome.com/file/2023/7/79317_0s.jpg)
Spring集成Redis详解代码示例
2023-05-04 00:58:25
![](https://img.aspxhome.com/file/2023/1/94341_0s.jpg)
详解Android App中创建ViewPager组件的方法
2023-07-12 00:46:14
JPA多数据源分布式事务处理方案
2023-08-09 03:50:06