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

边栏推荐
- RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
- CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
- 最近的工作
- An error row size too large (& gt; 8126) occurs when MySQL's MyISAM engine switches to InnoDB
- ESP8266 系统环境搭建
- SBC chip 35584 data manual pre regulator translation
- How to stack double and float in the bottom layer of C language
- FS4100 锂电充电管理IC输入12V给8.4V充电IC
- I drew several exquisite charts with plotly, which turned out to be beautiful!!
- 8.4v dual lithium battery professional charging IC (fs4062a)
猜你喜欢

Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
![[observation] shengteng Zhixing: scene driven, innovation first, press the](/img/53/29f5fd8b869087bafbe74849329c84.jpg)
[observation] shengteng Zhixing: scene driven, innovation first, press the "acceleration key" for Intelligent Transportation

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

Recommend a crud tool that may become the best crud tool in 2019 during the National Day

获取列表中最大最小值的前n个数值的位置索引的四种方法

localhost和127.0.0.1的区别?

hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618

SBC芯片35584数据手册预调节器翻译

Analysis on rendering principle of mobile terminal

一个10年左右的老程序员说:简单CRUD功能进入无码开发时代1 之 增删改查接口信息
随机推荐
ESP8266 系统环境搭建
Solving Bob's survival problem by trilogy routine
「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0
中衍期货公司是国内的正规平台吗?开户安全吗?想开个期货账户
Cloud native community boss blog
网上开期货账户安全吗?如何避免被骗?
uni-app自定义导航
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
2022 Devops roadmap | medium
HM3416H降压IC芯片PWM/PFM 控制 DC-DC 降压转换器
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
观点丨Play and Earn 会让加密游戏误入歧途
Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter
ResourceNotFoundException : Unable to find resource
Soft deletion of data - when? How to realize it?
Solution to the problem that JLINK CDC UART driver cannot be installed normally under win7 system
Build a BPMN modeling Web Service
如何使用物联网低代码平台进行工作表管理?
力扣1082,1084题解_sql查询类型的题目
国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具