5 Nisan 2008 Cumartesi

Why Doesn't PrintWriter Work?

Even though you may call method PrintWriter.println(), you may not get any output. This is because PrintWriter is auto-flush off by default. Check the code below:

 1 public class Test {
2 public static void main(String[] args) {
3 OutputStream out1 = System.out;
4 PrintWriter out = new PrintWriter(out1);
5 out.println("hello");
6 System.out.println("text is about to be flushed.");
7 out.flush();
8 }
9 }

The output is as follows:
text is about to be flushed.
hello

To enable auto-flush, you must initialize PrintWriter as follows:
PrintWriter out = new PrintWriter(out1, true);

Hiç yorum yok: