This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I was trying some very basic stuffs using input and output streams.
Here below is the snippet...
The above stated code snippet works fine if executed as such. But......
If I give "fos.write(fis.read())" instead of "fos.write(c)", the output file contains only some junk characters.
Can somebody explain me why this is so???
InputStream.read() reads one character, moves forward and then returns the read character. So if you call read() twice in the loop, once in the guard and once in the writing, then the first, third, fifth etc bytes are not written. Also, if there is an odd number of bytes then the guard will return something other than -1, but the read used in the write returns -1 and this is then written as a byte.
You could try without reading in the guard but you wouldn't know when to stop: