Search code examples
javaprintwriter

How to create a file with a different name every time


I am making a program which exports some text to a text file every time you click a button, i want it so that every time you clcik the button the text file has a new name so the text file doesn't get replaced every time. I've tried this code

 SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

PrintWriter writer = new PrintWriter(sdf + "Title", "UTF-8");
                writer.println("Test"");
                writer.println("Test"");
                writer.println("Test");
                writer.close();

But it didn't seem to work so is there any way to do it?


Solution

  • PrintWriter writer = new PrintWriter(sdf.format(new Date()) + "Title", "UTF-8");
    

    you should use like this.

    sdf - String, always constant.

    sdf.format(new Date()) - It will give unique value each time it will call.