• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.write('X') isn't working as expected.  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is supposed to print a character on the console :

But it does not print the character on the console unless I add or at the end of the code.

Why?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just how System.out.write works.

Why not use System.out.print()?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you putting the character in a byte? you should use char instead.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:That's just how System.out.write works.

Why not use System.out.print()?
I think everybody should avoid methods like System.out.write and System.in.read. Please check closely whether System.out.write actually declares that IOException.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:That's just how System.out.write works.?


From the Doc,

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.


What it doesn't say is, flushing is mandatory to print the character.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to know what flush() does and what "flushing" means.

Some I/O classes use buffers to make I/O more efficient. If you write a single byte to an output stream, it's not immediately written to the file, network or wherever the output stream is writing to. It's stored in a buffer instead. When enough bytes have been written, the whole buffer is written at once. Writing a whole block of data at once to for example a harddisk is much more efficient (much faster) than writing byte by byte.

The flush() method forces the output stream to write whatever is in the buffer, even if the buffer is not yet full.

This mechanism is used for the console too. If you write just one character, it will be buffered, and not be displayed until you either flush the output stream or write a newline character. (PrintStream, which is what System.out is, detects newline characters and automatically flushes its buffer when it sees one).
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Some I/O classes use buffers to make I/O more efficient.


How do I know if a class using buffer?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!