Note that there are some explanatory texts on larger screens.

plurals
  1. POhow write method in FileOutputStream truncates the int type argument
    text
    copied!<p>write() method in FileOutputStream takes an int but truncates the first 3 bytes and writes the byte to stream. If a file contains characters whose ASCII value is more than 127 and bytes are read from it and then written to output stream(another text file) how will it display it because in Java bytes can have a max value of +127.</p> <p>If a text file(input.text) has character '›' whose ASCII value is 155. An input stream,input, reads from it : <code>int in= new FileInputStream("input.txt").read();//in = 155</code></p> <p>Now it writes to another text file(output.txt)</p> <p><code>new FileOutputStream("output.txt").write(in);</code></p> <p>Here integer "in" is truncated to byte which will have corresponding decimal value : -101. How it successfully manages to write the character to file even though information about it seems to have been lost?</p> <p>Just now i went through the description of write(int) method in java docs and what i observed was</p> <blockquote> <p>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 <strong>ignored</strong>.</p> </blockquote> <p>So i believe that contrary to what i thought earlier(the int in write() is truncated as would happen while downcasting an integer to byte for values greater than 127) the 24 high order bits are only ignored and only 8 least significant bits are considered. No truncation and conversion to byte occurs. I guess i am correct.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload