• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just how System.out.write works.

Why not use System.out.print()?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you putting the character in a byte? You should use char instead.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic