Question

Given a byte array I can make a file from it with the code

Files.write(String path, byte[] byteArray)

The problem is, since I have to work with extremely large array sizes, and I can't make an array with a long size

long n = 100000000;
byte byteArray = new byte[n]; //ERROR! Required int, found long

what I do is I have several byte arrays. For example, since I can't have a 4gb array, I make 20 byte arrays of 200mb. These 20 arrays represent the information of a file. Now, how can I create a file from this byte arrays? The only way I know is using Files.write, but that only works with 1 array.

Thanks in advance.

Was it helpful?

Solution

You could use:

Files.write(path, bytes, StandardOpenOption.APPEND);

On the calls after the first one.

A better method (that doesn't require re-opening the file 20 times) would be using a FileOutputStream like:

FileOutputStream fos = new FileOutputStream(path);
for(int i = 0; i < byteArrays.length; ++i)
{
    fos.write(byteArrays[i]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top