• Post Reply Bookmark Topic Watch Topic
  • New Topic

How read into byte array when file size is of type long?  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to read a file into a byte array, but the problem is I can't create an array big enough to ensure I get all data. A file's size could be longer than an int and arrays can only be created of size int:
byte b = new byte[ int_value ];
What can/should I do? Should I just have a while loop that does a check on available and creates a new byte array of that size and then fills it, then checks avail again, etc? Then put all the byte arrays into a List or something?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert, are you sure you will actually be able accommodate more than 2GB worth of byte arrays in the first place? You'd need to be running your JVM on a 64-bit architecture for this to work, and of course you'll have to have the actual memory as well.
Do you really have to buffer all of the file?
- Peter
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
You may want to look into the new java.nio packages for working with huge files. The java.nio.channels.FileChannel class gives you access to all the bytes in the file similar to a byte array, although accessing those bytes will be a bit more tedious. You can also map portions of the file directly into memory for a performance boost.
Michael Morris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!