KYorke
KYorke

Reputation: 23

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?

Upvotes: 1

Views: 44

Answers (2)

VioletTec
VioletTec

Reputation: 71

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());

Upvotes: 1

Prog_G
Prog_G

Reputation: 1615

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();
    }

Upvotes: 0

Related Questions