【问题标题】:How to check the new imagename generated doesn't exists to prevent overriding?如何检查生成的新图像名不存在以防止覆盖?
【发布时间】:2017-06-13 02:07:26
【问题描述】:

在存储文件之前,我会检查文件名是否已经存在(以防止覆盖)

为此,我使用以下代码

以下代码的问题是不能保证生成的新图像不存在。

public static String checkIfFileExists(String image_name,String from) throws IOException
        {
        String newimage = "";
        String path="";
        Properties props_load = Utility.getProperties();
       path = props_load.getProperty("videopath"); 
        File file =  new File(path+image_name);
        if (file.exists())  // **if file name already exists**
        { 
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(1000);
        Matcher matcher = Pattern.compile("(.*)\\.(.*?)").matcher(image_name);
        if (matcher.matches()) {  // <== test input filename is OK?
        newimage = String.format("%s_%d.%s", matcher.group(1), randomInt, matcher.group(2));
        }
        }
        else
        {
        newimage = image_name;
        }
        return newimage;
        }

【问题讨论】:

  • 如何检查新文件是否不存在?再次使用File.exists()(请注意,new File(path) 不会创建文件,而只是处理它,除非您实际写入该文件,否则不会在文件系统上创建任何内容)。如何防止名称冲突或至少减少它们?使用UUID
  • 以上所有,更改名称后,您必须使用新名称重新测试。
  • 你能不能简单地遍历你的 newImage 名称以重试它是否存在?在混合物中添加一个计数器以防止死循环。你甚至可以为该任务编写一个递归调用,让它看起来很漂亮
  • @fildor 正如他所说,让它看起来很花哨。这当然是很糟糕的推理。

标签: java


【解决方案1】:

createNewFile,仅此而已!

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
public static void main(String args[]){
    try{
        System.out.println(checkIfFileExists("test.png",""));
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static String checkIfFileExists(String image_name,String from) throws IOException
    {
    String newimage = "test.png";
    String path="D:/";
    //Properties props_load = Utility.getProperties();
    path = "D:/";//props_load.getProperty("videopath"); 
    File file =  new File(path+image_name);
    if (!file.createNewFile())  // **if file name already exists**
    { 
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(1000);
    Matcher matcher = Pattern.compile("(.*)\\.(.*?)").matcher(image_name);
    if (matcher.matches()) {  // <== test input filename is OK?
    newimage = String.format("%s_%d.%s", matcher.group(1), randomInt, matcher.group(2));
    }
    }
    else
    {
    newimage = image_name;
    }
    return newimage;
    }

}

【讨论】:

    【解决方案2】:

    为什么不使用 File#createNewFile() ?

    只有在没有具有给定名称的文件时才创建一个文件。 在循环中使用它并更改循环内的文件名直到创建成功应该可以解决问题。 类似的东西

    boolean created = false;
    File file;
    while (!created) {
      file = new File(randomfilename);
      created = file.createNewFile();
    }
    
    // write file

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多