0

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?

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Right, i'd love to do that, without having to go into command prompt. Rather simple way of copying the console output from my ide (visual studio code) into a seperate output.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.