将新文件添加到文件夹

Adding new files to a folder

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

谁能帮我解决这个问题?

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

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

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

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

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

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