This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert java.io.File to ByteArryInputStream?
 
Similar Threads
Create Directory on Server after making a socket connection ?
How to cut a file from one location
How well to list the content of a directory in java?
How to Convert a java.io.File to struts FormFile?
Program to check whehter a file is read-only / write