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?
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
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
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
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher