This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is int required as FileOutputStream's argument?

 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

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???

Regards,
Ram
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for one thing you would only be writing every other byte from the input to the output, instead of every byte.
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul/other ranchers,

Thanks for replying...
But still, I did not get what you wanted to communicate.
Could you please elaborate it for me?

Regards,
Ram
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob. That was pretty good explanation
-Ram
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic