javaoutputstreamprintstream

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


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?)


Solution

  • 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