【问题标题】:How to create a new file with additional incrementing number when file already exists?当文件已经存在时,如何创建一个带有额外递增数字的新文件?
【发布时间】:2015-10-20 09:00:14
【问题描述】:

我有一小段代码

String fileName = "test.txt"
Path path = Paths.get(fileName);
File f = null;
if (Files.exists(path)) {
    LOGGER.warn("File allready exists!");
    f = new File("COPYOF-" + fileName);
} else {
    f = new File(fileName);
}

它有效,但它没有做我想要它做的事情......

我想以“正确”的方式来做。

首次下载时,文件应命名为test.txt。第二次 - test(1).txt。第三个:test(2).txt

目前它正在以test.txt 的形式下载它,第二次将其命名为COPYOF-test.txt,而在第三次尝试时它只是覆盖了COPYOF-test.txt 文件。

我正在寻找一种适当的方法来实施此解决方案,但我不知道该怎么做...

【问题讨论】:

标签: java file


【解决方案1】:

工作代码:

String fileName = "test.txt";

String extension = "";
String name = "";

int idxOfDot = fileName.lastIndexOf('.');   //Get the last index of . to separate extension
extension = fileName.substring(idxOfDot + 1);
name = fileName.substring(0, idxOfDot);

Path path = Paths.get(fileName);
int counter = 1;
File f = null;
while(Files.exists(path)){
    fileName = name+"("+counter+")."+extension;
    path = Paths.get(fileName);
    counter++;
}
f = new File(fileName);

说明:

  1. 首先将extensionfile name without extension分开并设置counter=1然后检查这个文件是否存在。如果存在则转到第 2 步,否则转到第 3 步。

  2. 如果文件存在,则使用file name without extension+(+counter+)+extension 生成新名称并检查此文件是否存在。如果存在,则以增量 counter 重复此步骤。

  3. 在这里使用最新生成的文件名创建文件。

【讨论】:

    【解决方案2】:
    String fileName = "test.txt";
    String[] parts = fileName.split(".");
    Path path = Paths.get(fileName);
    File f = null;
    int i = 1;
    while (Files.exists(path)) {
        LOGGER.warn("File allready exists!");
        i++;
        path = Paths.get(parts[0] + "(" + i + ")" + parts[1]);
    } 
    f = new File(parts[0] + "(" + i + ")" + parts[1]);
    

    PS:我添加了一个特定的方法来增加文件名(字符串文件名)。它会更干净的代码。您将能够检查特定情况(带点的文件,montextpartie(1)et(3).txt...等

    【讨论】:

    • String[] 部分 = fileName.split(".");将允许一个变量文件名...然后 f = new File(parts[0] + "(" + i + ")"+parts[1]);
    • @Margu 除非文件有多个.,比如test.tar.gz;在这种情况下,它会“忘记”部分文件名/扩展名。
    • @tobias_k 是的,更好的是 Zaman 的答案,现在也是公认的答案。
    【解决方案3】:

    概括Fundhor的回答:

    String fileName = "test.txt";
    ArrayList<String> fileNameArray = Arrays.asList(fileName.split("\\."));
    String fileExtension = fileNameArray.remove(fileNameArray.size() - 1);
    StringBuilder strBuilder = new StringBuilder();
    while (fileNameArray.size() != 0) {
       strBuilder.append(fileNameArray.remove(0));
    }
    String fileNameWithoutExtension = strBuilder.toString();
    Path path = Paths.get(fileName);
    File f = null;
    int i = 1;
    while(Files.exists(path)) {
        LOGGER.warn("File already exists!");
        fileName = fileNameWithoutExtension + "(" + i + ")" + "." + fileExtension;
        path = Paths.get(fileName);
    }
    f = new File(fileName);
    

    我的回答还允许文件名中包含句点,例如part1.part2.extension

    【讨论】:

      猜你喜欢
      • 2014-02-09
      • 2017-12-25
      • 2022-12-16
      • 2012-08-24
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多