当前位置:网站首页>【技术碎片】重名文件 加后缀重命名过滤实现
【技术碎片】重名文件 加后缀重命名过滤实现
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);
}
}
}
运行

边栏推荐
- 性能测试方案(计划)模板
- 首批!青藤通过信通院CWPP能力评估检验
- HW blue team intermediate interview reply
- 2022.05.26 (lc_1143_longest common subsequence)
- 站在今天这样一个时间节点上,或许对产业互联网有一个更加明晰的认识
- Batch detection of specified ports of different URLs (py script)
- How to realize face verification quickly and accurately?
- I drew several exquisite charts with plotly, which turned out to be beautiful!!
- 2022 Devops roadmap | medium
- Key points of lldp protocol preparation
猜你喜欢

How do various embedded functions in VR panoramic works be achieved?

测试apk-异常管控netLocation攻击者开发

localhost和127.0.0.1的区别?

Spark ShuffleManager

C (pointer 02)

2022.05.26 (lc_1143_longest common subsequence)

Trilogy to solve the problem of playing chess first and then

One article explains in detail the exploration and practice of eventmesh landing on Huawei cloud

I drew several exquisite charts with plotly, which turned out to be beautiful!!

融入机器学习,让Chrome浏览器更“懂”你
随机推荐
【录入课本latex记录】
I drew several exquisite charts with plotly, which turned out to be beautiful!!
Mongodb index unique
Solution to the problem that JLINK CDC UART driver cannot be installed normally under win7 system
frp reverse proxy
融入机器学习,让Chrome浏览器更“懂”你
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
localhost和127.0.0.1的区别?
如何使用物联网低代码平台进行工作表管理?
C语言 浮点数 储存形式
NFS network mount to create server image
Spark ShuffleManager
历时2年442位作者132个机构!Google发布语言模型评价新基准BIG-bench,204个任务全面评价语言模型能力,附论文
用 Plotly 绘制了几张精湛的图表,美翻了!!
企业级存储发展趋势谈:开源存储的冷思考
Harbor镜像拉取凭证配置
Fs4521 constant voltage linear charging IC
Logback exclude specified package / class / method log output
软件定义边界(SDP)
2022.05.28 (lc_5_longest palindrome substring)