Java - How to use PrintStream/OutputStream to print to the Command Line

user3211306 picture user3211306 · Aug 1, 2018 · Viewed 8.5k times · Source

I know we can use PrintStream to print lines to a given file:

    PrintStream output;
    output = new PrintStream("./temp.txt");
    output .println("some output text"); 

However, can we use PrintStream to print lines to the command line?

I've looked through the Java docs and it seems PrintStream constructor can take a file path or an OutputStream (is there a OutputStream subclass that would print to command line?)

Answer

Nin picture Nin · Aug 1, 2018
output = new PrintStream(System.out);

or actually,

output = System.out;

Similarly, you can do the same with System.err ... So why don't we just simply use System.out and System.err directly? sout + tab is quite fast to type in IntelliJ