Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWriter in java giving unexpected behaviour

import java.io.*;
class demo
{
public static void main(String args[])
{
    PrintWriter pw=new PrintWriter(System.out);
    pw.println("java");
    //pw.print("java");
}
}

// the output is java using pw.println but output is null using pw.print i.e nothing gets printed on console while using print.

like image 749
shubhendu mahajan Avatar asked Mar 01 '26 23:03

shubhendu mahajan


1 Answers

Try this instead :

PrintWriter pw=new PrintWriter(System.out);
pw.print("java");
pw.flush();

The PrintWriter is going to be doing internal buffering, and the println method is automatically flushing it.

like image 151
stevevls Avatar answered Mar 04 '26 11:03

stevevls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!