添加新文件到文件夹中。

huangapple go评论78阅读模式
英文:

Adding new files to a folder

问题

我已经创建了一个区块链解决方案,原则上运行良好,但我需要每次代码被执行时,都在文件夹中创建一个新文件。目前它只会不断地覆盖该文件。

有人可以帮我解决这个问题吗?

英文:

I have created a blockchain solution and that works fine in principle but I need it so that every time the code gets executed, a new file is created in the folder. Currently it just keeps overwriting the file.

Can anyone help me out on this?

答案1

得分: 1

也许在创建文件时可以获取当前时间戳。时间戳是唯一的,因此不会出现覆盖的问题。

File f = new File("yourFileName" + System.currentTimeMillis());
英文:

Maybe you can get the current timestamp when creating the file. The timestamp is unique, so it will not have the problem of overwriting.

File f = new File("yourFileName"+System.currentTimeMillis());

答案2

得分: 0

下面的代码应该可以完成您的工作。您可以通过使用事务ID作为文件名,将新文件名保持唯一。

String dir = "Some Path";
File test = new File(dir + getFileName());
try {
    test.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
}

您可以使用以下代码获取唯一的文件名:

public String getFileName() {
    return UUID.randomUUID().toString();
}
英文:

The below code should do your work. you can keep the new Filename as unique by using transaction id as the fileName.

String dir = "Some Path";
        File test = new File(dir + "newFileName");
        try {
            test.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

You can use the below code to get unique fileName:

public String getFileName() {
        return UUID.randomUUID().toString();
    }

huangapple
  • 本文由 发表于 2020年3月16日 20:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定