重命名zip文件Java中的重复文件 [英] Rename duplicate files in a zip file Java

查看:769
本文介绍了重命名zip文件Java中的重复文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文件,包括重复文件,我必须将其压缩到存档中.您知道一些能够在创建存档文件ex(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

请记住,重复项的数量应小于或等于给定字符串数组中的替代名称.您可以通过同时将循环修改为:

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中的重复文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆