当前位置:网站首页>[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

边栏推荐
- How to use the low code platform of the Internet of things for worksheet management?
- Recent work
- FPGA状态机
- Performance test plan (plan) template
- Spark ShuffleManager
- C language floating point number storage form
- Build a BPMN modeling Web Service
- NFS network mount to create server image
- 首批!青藤通过信通院CWPP能力评估检验
- hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618
猜你喜欢

8.4v dual lithium battery professional charging IC (fs4062a)

SBC chip 35584 data manual pre regulator translation

Performance test plan (plan) template

Flutter series: UI layout introduction

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

用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性

江波龙 FORESEE XP2000 PCIe 4.0 SSD 多重加密功能,锁定数据安全

2022 Devops roadmap | medium

今年高考期间各考点秩序井然,未发生影响安全的敏感案事件

One question to explain the past and present life of dynamic planning
随机推荐
[Legendre] polynomial
推开混合云市场大门,Lenovo xCloud的破局之道
It took 2 years, 442 authors and 132 institutions! Google released the new benchmark big bench for language model evaluation. 204 tasks comprehensively evaluated the language model ability, with paper
最近的工作
20192407 2021-2022-2 《网络与系统攻防技术》实验八实验报告
HM3416H降压IC芯片PWM/PFM 控制 DC-DC 降压转换器
利用阿里云国际购买的服务器搭建个人网站步骤
PDU会话流程
ESP8266 系统环境搭建
Build a BPMN modeling Web Service
Cloud native community boss blog
LLDP协议编写要点
Flutter series: UI layout introduction
MBA-day21 线性规划问题
户外太阳能野营灯移动电源方案
【技术碎片】重名文件 加后缀重命名过滤实现
FS4521恒压线性充电IC
8.4v dual lithium battery professional charging IC (fs4062a)
身份识别与访问管理(IAM)
2022 Devops roadmap | medium