英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论