当前位置:网站首页>【技术碎片】重名文件 加后缀重命名过滤实现
【技术碎片】重名文件 加后缀重命名过滤实现
2022-06-10 19:21:00 【锥栗】

前言
重名文件加后缀重命名过滤
如果存在重名,则在后面(扩展名的前面)加后缀 “_1”, “_2”, “_3”, … 直到文件名不重复为止。
实现
public class Main {
public static void main(String[] args) {
String originalFileName = "1232121421412(0).jpg";
Boolean repeat = true;
// 文件是否存在重名
StringBuilder fileName = new StringBuilder();
// 如果存在重名,则加_1后缀
if (repeat) {
int suffixNumber = 0;
String fileExtensionName = "";
// 分离文件拓展名(.jpg .png ... )
for (int i = originalFileName.length() - 1; i > 0; i--) {
if (originalFileName.charAt(i) == '.') {
fileExtensionName = originalFileName.substring(i, originalFileName.length());
fileName = new StringBuilder(originalFileName.substring(0, i));
break;
}
}
// 加_1后缀
fileName.append("_" + ++suffixNumber);
// 如果已经存在 _1 后缀,则改为 _2 , _3 ,...直到不重复为止
while (repeat) {
// 把我们添加的"_"后面所有数字字符删除
for (int length = fileName.length(); length > 0; length--) {
char c = fileName.charAt(length - 1);
if (c != '_') {
fileName.delete(length - 1, length);
}
else {
break;
}
}
fileName.append(++suffixNumber);
// 假设_23时不再重复
if ("23".equals(fileName.substring(fileName.length() - 2, fileName.length()))) {
break;
}
}
// 合并扩展名后返回
fileName.append(fileExtensionName);
System.out.println(fileName);
}
}
}
运行

边栏推荐
- uni-app自定义导航
- 20192407 2021-2022-2 《网络与系统攻防技术》实验八实验报告
- Change the root
- 如何使用物联网低代码平台进行工作表管理?
- NFS network mount to create server image
- Fs4100 lithium battery charging management IC input 12V to 8.4v charging IC
- Kp522201a adopts SOT23-6 encapsulated 4.5V to 17V input, 2A output, 600kHz synchronous step-down converter
- Solving Bob's survival problem by trilogy routine
- 网上开期货账户安全吗?如何避免被骗?
- How do big factories write data analysis reports?
猜你喜欢

用 Plotly 绘制了几张精湛的图表,美翻了!!

批量检测不同url的指定端口(py脚本)

2022.05.29 (lc_6079_price reduction)

ZABBIX server trapper Remote Code Execution Vulnerability (cve-2017-2824)

KP522201A采用 SOT23-6 封装的 4.5V 至 17V 输入、2A 输出、600kHz 同步降压转换器

One question to explain the past and present life of dynamic planning

Redis集群形式--哨兵模式集群和高可用模式集群---Redis学习笔记003

HM3416H降压IC芯片PWM/PFM 控制 DC-DC 降压转换器

How to use the low code platform of the Internet of things for worksheet management?

LLDP协议编写要点
随机推荐
How do big factories write data analysis reports?
Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter
Mongodb 唯一索引
Basic instructions for ads and AXD
2022 年 DevOps 路线图|Medium
nodejs: 官方文档3 dgram Stream
刷脸认证如何实现人脸又快又准完成校验?
2022.05.25 (lc_718_longest repeating subarray)
How to use the low code platform of the Internet of things for worksheet management?
In the all digital era, how can enterprise it complete transformation?
SBC芯片35584数据手册预调节器翻译
Complete knapsack problem and optimization tips
flink啥时候可以支持在sql-client模式下,往yarn上提交任务可以指定applicati
Bit operation topic
在VR全景中如何添加聚合热点?内容模块如何添加?
零信任架构
On the development trend of enterprise storage: cold thoughts on open source storage
Dock/rancher2 deploy redis:5.0.9
2022.05.29 (lc_6079_price reduction)
FS4060A是4.2V/3A充电IC