• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

read() and write() in I/O - Michael or somebody explain

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain the read() and write operations in the I/O package classes/sub-classes (specifically for Input/OutputStream classes.
API doc says "write(int b)=> Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."
Similarly, read(), reads the next byte and returns as an int.!!!
Say, if i pass int i = 123456789 to write() and then try to read() the same, can somebody explain what happens?
It seems from the docs, that the 24 high-order bits of 123456789 are going to be ignored!! Then, how is my read operation able to reconstruct and give me back 123456789!!???

Thanks
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Similarly, read(), reads the next byte and returns as an int.!!!
Say, if i pass int i = 123456789 to write() and then try to read() the
same, can somebody explain what happens?
It seems from the docs, that the 24 high-order bits of 123456789 are
going to be ignored!! Then, how is my read operation able to
reconstruct and give me back 123456789!!???


You will get the 0x15 (the last byte of integer 123456789)
back. Input/Output stream interfaces are byte streams. They
specifes how bytes are written/read only.
DataInput & DataOutput interfaces have handy methods to
deal with all primitive types.
 
That new kid is a freak. Show him this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!