Reading remote file into byte array

All,

We have a need to read a file into a byte array.  These files are large (can be up to 100 MB) so we need something very efficient/quick. I've seen some examples, but I'm looking for what the experts would consider the fastest way to do this.  A code snippet.

As said above we want to read these from a remote file share (this is all running on Windows), so we need to read from a \\fileserver\folder\filename.txt type of syntax.

Will give all 500 points to anyone providing a short working sample.  We've seen some out there, but don't know if: 1, they work with a remote file; 2, if they're the most efficient approach.

Thanks
LVL 1
Gene KlamerusTechnical ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
The class at the link here has also a fileToByteArray method:

http://technojeeves.com/joomla/index.php/free/93-file-to-string-in-java
Gene KlamerusTechnical ArchitectAuthor Commented:
This looks pretty efficient.  Fewest lines I've seen.

How would I use this with a remote Windows file though?  Can you specify the syntax for that?  I needed that too.
CEHJCommented:
>>How would I use this with a remote Windows file though?

Well you answered that yourself ;) Use something like the below. For huge file, nio might be faster, so you could try


http://fisheye.exoplatform.org/browse/~raw,r=24747/projects/jcr/trunk/component/core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/TransientValueData.java
File remote = new File("//servername/sharename/folder/file.txt");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

CEHJCommented:
:)
Gene KlamerusTechnical ArchitectAuthor Commented:
Yes these files will be large.  Some will be in the 100-150 MB range.

Would you recommend nio then?  Even for across the network?
Mick BarryJava DeveloperCommented:
Mick BarryJava DeveloperCommented:
> This looks pretty efficient.  Fewest lines I've seen.

its actually about the least efficient
CEHJCommented:
>>Would you recommend nio then?  Even for across the network?

Yes - it's worth trying. You can of course benchmark both
Gene KlamerusTechnical ArchitectAuthor Commented:
By what measure is it more efficient?

I'm not looking for fewest lines (although I said that).  I'm looking for the fastest and the least resources consumed (particularly memory).
Mick BarryJava DeveloperCommented:
use the link I posted, converting an entire file into a string as suggested earlier will be slow and require additional memory

if you want to read the string data then instead use something like this

http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line
Gene KlamerusTechnical ArchitectAuthor Commented:
thanks,

We're not going to go all the way to creating a string though (from the original posting).  We're going to stop with the byte array since that's what we need.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.