Tomasteel
Tomasteel

Reputation: 21

Is there a way to simply copy console output into new text file?

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

Answers (1)

tatsukenji
tatsukenji

Reputation: 73

Option 1: Output via Terminal

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

Option 2: Generate file via code

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

Related Questions