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
ie nothing gets printed on console while using print
.
It's almost certainly just buffering - and as you're not flushing it, you never get the output. From the docs :
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.
Try:
pw.flush();
at the end of the code.
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.
对于自动刷新,您可以使用此构造函数
PrintWriter(OutputStream out, boolean autoFlush);
A call to println()
implicitly flushes the output buffer whereas a call to print()
does not. Try using print()
and then call pw.flush()
.
Note also that there are constructors of PrintWriter which include an option to automatically flush after any write call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.