• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving InputStream to File  RSS feed

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am obtaining an InputStream (That contains XML Data) from a source and i intend to save this into a file, Please can someone offer me a suggestion
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You read the bytes from the input stream, and save them to a file, in a loop. What part are you having trouble with?
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually copying from a InputStream or Reader to an OutputStream or Writer goes as follows:

Fortunately, the methods are also called read and write. Now there are multiple read/write methods, but all of them should be fine. From experience it's faster to read multiple bytes each time instead of byte per byte.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob:
Usually copying from a InputStream or Reader to an OutputStream or Writer goes as follows:



Since, jdk 1.4, FileChannel provides two very useful methods transferTo and transferFrom. These methods transfer data from one channel to another and thus reduces the amount of coding/testing for such often used routines.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are only possible if you can get Channel objects for both sides of the transfer. That's not always possible.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!