Reputation: 21
I had a very simple request, I have a full code that sorts through an input file and outputs the merged strings properly into the console. My question is can I take this already perfect output from my CONSOLE and simply output it into a seperate .txt fle as well?
I've tried doing a simple PrintStream printStream = new PrintStream(new FileOutputStream("output.txt")); System.setOut(printStream);
it does create my output.txt file, but it's always empty?
Upvotes: 0
Views: 1868
Reputation: 73
Since your script already outputs perfectly to the terminal, you can redirect that output to a file via >
$ ./my-script > output.txt
You can also use the tee
command if you still want to see the output in your terminal.
$ ./my-script | tee output.txt
I'm not too familiar with Java / PrintStream but from https://www.tutorialspoint.com/java/io/printstream_print_string.htm, you should see content in your file by:
PrintStream printStream = new PrintStream(new FileOutputStream("output.txt"));
printStream.print("foo");
printStream.flush();
Upvotes: 1