Reputation: 1213
I'm starting to learn Java and I didn't understand why this code doesn't work:
import java.io.*;
public class Principal
{
public static void main (String args[]) throws IOException
{
OutputStream outConsole = System.out;
outConsole.write(101);
}
}
System.out is an PrintStream object, PrintStream is a subclass of OutputStream. OutputStream is an abstract class and write() is an abstract method of it. So I guess PrintStream must have the write method implemented, why this code doesn't work, then?
Upvotes: 1
Views: 1299
Reputation: 229088
System.out is a PrintSteam, which is line buffered. Flush it
outConsole.flush();
Upvotes: 1
Reputation: 279950
PrintStream#write(int)
doesn't automatically flush the stream under all conditions. The javadoc states
Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.
In any other case, you need to do so explicitly
outConsole.flush();
Upvotes: 4