I am trying to figure out how to copy standard output to a file while also having it continue to go to standard output. I can that with bash, but I want to do it within my Scala program (at the start of main) because that is where the desired output file name will be known. I found the following way to redirect standard output to a file in Java, but that stops it from going to standard output. Does anyone know a simple way to do this? Thanks.
PrintStream console = System.out; File file = new File("out.txt"); FileOutputStream fos = new FileOutputStream(file); PrintStream ps = new PrintStream(fos); System.setOut(ps); System.out.println("This goes to out.txt"); System.setOut(console); System.out.println("This goes to the console");