Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert java.io.File to ByteArryInputStream?

 
myron schabe
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I convert a java.io.File to a ByteArrayInputStream?
Many Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to do that? A ByteArrayInputStream doesn't seem to have any methods that any other InputStream doesn't have. It just has the additional ability (or restriction in this case) that it reads input from an existing byte array. Which means you would need to create the byte array first, since it doesn't exist initially. Why bother? I mean, you can do it:

But isn't this easier?

The only advantage I see to the former method is that a ByteArrayInputStream will never block, and will always be able to read all bytes you ask for up to the end of the byte array. This may be useful or necessary for some applications. If not though, I'd keep things simple where possible, and just use the BufferedInputStream technique.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic