• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to write the bytes in OutputStream to file?

 
Anthony Karta
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a method from API that return OutputStream, and I want to write that stream to a file? how to do that?

many thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that once the OutputStream is created you cannot change the destiny of the bytes (i.e. memory or file).

For instance if I create an OutputStream this way:



How could I possibly make that this stream is written to a file? I daresay there is no way to do that.

However, since the OutputStream is used to write bytes, you can write those same bytes in a FileOutputStream after or before you have writen them into the returned OutputStream. In other words, you write your bytes in both streams.



Does that help?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic