-1

I am trying to redirect the console input to a file. Problem is that every time i create a file it overwrites it or creates new files if I select the name of file to include unix timestamp. I saw similar questions here but I am not sure which approach or class to use.

    PrintStream out;
    PrintStream oldout = new PrintStream(System.out);

    try {
        out = new PrintStream(
                new FileOutputStream(
                        workFolder + File.separator + "output" + Instant.now().getEpochSecond() + ".txt"));


         System.setOut(out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
System.setOut(oldout);

So if there isn't a file to create it, but if there is already a file to just append new data, but not overwrite or create new files.

Georgi Michev
  • 764
  • 5
  • 18
  • 37

1 Answers1

-1

As per Java docs

public FileOutputStream(String name,
                boolean append)
                 throws FileNotFoundException

Parameters: name - the system-dependent file name

append - if true, then bytes will be written to the end of the file rather than the beginning

There is a constructor which allows passing the boolean value which decides whether to append the data in file or not.

You can use it.

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62