wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Convert java.io.File to ByteArryInputStream? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Convert java.io.File to ByteArryInputStream?" Watch "Convert java.io.File to ByteArryInputStream?" New topic
Author

Convert java.io.File to ByteArryInputStream?

myron schabe
Greenhorn

Joined: Aug 21, 2002
Posts: 19
How can I convert a java.io.File to a ByteArrayInputStream?
Many Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Convert java.io.File to ByteArryInputStream?
 
Similar Threads
Program to check whehter a file is read-only / write
How to Convert a java.io.File to struts FormFile?
How to cut a file from one location
Create Directory on Server after making a socket connection ?
How well to list the content of a directory in java?