问题描述
我有几个文件,包括重复文件,我必须将它们压缩到一个存档中.你知道有什么工具可以在创建存档文件之前重命名重复文件吗?(cat.txt、cat(1).txt、cat(2)).txt ...)?
I have several files including duplicates which I have to compress into an archive.Do you know some tool able to rename duplicate files before creating the archive ex(cat.txt, cat(1).txt, cat(2).txt ...)?
推荐答案
我创建了以下代码,可以轻松删除重复项:
I have created the following code that easily removes duplicates:
static void renameDuplicates(String fileName, String[] newName) {
int i=1;
File file = new File(fileName + "(1).txt");
while (file.exists() && !file.isDirectory()) {
file.renameTo(new File(newName[i-1] + ".txt"));
i++;
file = new File(fileName + "(" + i + ").txt");
}
}
使用也很简单:
String[] newName = {"Meow", "MeowAgain", "OneMoreMeow", "Meowwww"};
renameDuplocates("cat", newName);
结果是:
cat.txt -> cat.txt
cat(1).txt -> Meow.txt
cat(2).txt -> MeowAgain.txt
cat(3).txt -> OneMoreMeow.txt
请记住,重复的数量应该小于或等于给定字符串数组中的替代名称.您可以通过 while 循环修改来防止它:
Keep on mind that the number of duplicates should be smaller or equal than alternative names in the array of string given. You can prevent it with while cycle modification to:
while (file.exists() && !file.isDirectory() && i<=newName.length)
在这种情况下,其余文件将保持未命名.
In this case the remaining files will keep unnamed.
这篇关于重命名 zip 文件中的重复文件 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!