当前位置:网站首页>[technical fragment] implementation of renaming and filtering duplicate name files with suffixes
[technical fragment] implementation of renaming and filtering duplicate name files with suffixes
2022-06-10 20:29:00 【Castanea henryi】

Preface
Rename and filter files with duplicate names and suffixes
If there is a duplicate name , It's in the back ( Before extension ) suffix “_1”, “_2”, “_3”, … Until the file name does not repeat .
Realization
public class Main {
public static void main(String[] args) {
String originalFileName = "1232121421412(0).jpg";
Boolean repeat = true;
// Whether the file has a duplicate name
StringBuilder fileName = new StringBuilder();
// If there is a duplicate name , Then add _1 suffix
if (repeat) {
int suffixNumber = 0;
String fileExtensionName = "";
// Separate file extensions (.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;
}
}
// Add _1 suffix
fileName.append("_" + ++suffixNumber);
// If it already exists _1 suffix , Is changed to _2 , _3 ,... Until not repeated
while (repeat) {
// Add our "_" All subsequent numeric characters are deleted
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);
// hypothesis _23 Do not repeat when
if ("23".equals(fileName.substring(fileName.length() - 2, fileName.length()))) {
break;
}
}
// Merge extensions and return
fileName.append(fileExtensionName);
System.out.println(fileName);
}
}
}
function

边栏推荐
- Performance test plan (plan) template
- Redis集群形式--哨兵模式集群和高可用模式集群---Redis学习笔记003
- 【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
- Solving Bob's survival problem by trilogy routine
- Microsoft Word tutorial "5", how to change the margins and create a newsletter column in word?
- NFS network mount to create server image
- One question to explain the past and present life of dynamic planning
- Flutter series: UI layout introduction
- MySQL数据库基础
- 测试apk-异常管控netLocation攻击者开发
猜你喜欢

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

首批!青藤通过信通院CWPP能力评估检验

CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)

京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q

在阿里云国际上使用 OSS 和 CDN 部署静态网站

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

移动端渲染原理浅析

HW blue team intermediate interview reply

国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具

【观察】昇腾智行:场景驱动,创新先行,为智慧交通按下“加速键”
随机推荐
Trilogy to solve the problem of playing chess first and then
Kp522201a adopts SOT23-6 encapsulated 4.5V to 17V input, 2A output, 600kHz synchronous step-down converter
Problem with makefile: unable to detect file updates
How to stack double and float in the bottom layer of C language
Is it safe to open a futures account online? How to avoid being cheated?
融入机器学习,让Chrome浏览器更“懂”你
On the development trend of enterprise storage: cold thoughts on open source storage
移动端渲染原理浅析
力扣1082,1084题解_sql查询类型的题目
使用环绕通知对目标方法进行增强—摘抄笔记
批量检测不同url的指定端口(py脚本)
latex tips 绝对值的竖线 \left|\right|
CVPR 2022丨清华大学提出:无监督域泛化 (UDG)
NetWorkX使用方法及 nx.draw()相关参数
获取列表中最大最小值的前n个数值的位置索引的四种方法
NFS network mount to create server image
Zabbix_原理架构-安装部署-自定义监控
【legendre】多项式
Complete knapsack problem and optimization tips
First batch! Sinomenine has passed CWPP capability assessment and inspection of Xintong Institute