Java 如何检查生成的新imagename';t是否存在以防止覆盖?

Java 如何检查生成的新imagename';t是否存在以防止覆盖?,java,Java,在存储文件之前,我检查文件名是否已经存在(以防止覆盖) 为此,我使用以下代码 下面代码的问题是,不能保证生成的newimage不存在 public static String checkIfFileExists(String image_name,String from) throws IOException { String newimage = ""; String path=""; Properties props_load

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

为此,我使用以下代码

下面代码的问题是,不能保证生成的newimage不存在

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;
        }
publicstaticstringcheckiffileexists(stringimage\u name,stringfrom)抛出IOException
{
字符串newimage=“”;
字符串路径=”;
Properties props_load=Utility.getProperties();
path=props_load.getProperty(“videopath”);
文件文件=新文件(路径+图像名称);
如果(file.exists())/**如果文件名已经存在**
{ 
Random randomGenerator=新的Random();
int randomInt=randomGenerator.nextInt(1000);
Matcher Matcher=Pattern.compile((.*)\.(.*)).Matcher(图像名称);

如果(matcher.matches()){/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;
    }
导入java.io.File;
导入java.io.IOException;
导入java.util.List;
导入java.util.Random;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公开课考试{
公共静态void main(字符串参数[]){
试一试{
System.out.println(checkIfFileExists(“test.png”和“”);
}捕获(例外e){
e、 printStackTrace();
}
}
publicstaticstringcheckiffileexists(stringimage\u name,stringfrom)引发IOException
{
字符串newimage=“test.png”;
字符串path=“D:/”;
//Properties props_load=Utility.getProperties();
path=“D:/”;//props_load.getProperty(“videopath”);
文件文件=新文件(路径+图像名称);
如果(!file.createNewFile())/**如果文件名已存在**
{ 
Random randomGenerator=新的Random();
int randomInt=randomGenerator.nextInt(1000);
Matcher Matcher=Pattern.compile((.*)\.(.*)).Matcher(图像名称);
如果(matcher.matches()){/为什么不使用文件#createNewFile()

只有当一个文件还不存在且具有给定名称时,它才可以创建一个文件。 在循环中使用它,并在循环中更改文件名,直到创建成功为止,应该可以做到这一点。 差不多

boolean created=false;
文件;
而(!已创建){
文件=新文件(随机文件名);
created=file.createNewFile();
}

//写入文件
使用createNewFile如何检查新文件是否也不存在?再次使用
文件.exists()
(请注意,
新文件(路径)
不创建文件,只需处理,除非您真的写入该文件,否则不会在文件系统上创建任何内容)。如何防止名称冲突或至少减少名称冲突?使用
UUID
。以上所有内容以及更改名称后,您必须使用新名称重新测试。如果新图像名称存在或不存在,您是否可以简单地循环您的新图像名称以重试?在混合中添加一个计数器以防止死锁。您甚至可以为该任务编写一个递归调用以进行测试看起来fancy@fildor正如他所说,让它看起来很花哨。这当然是可怕的推理。