Reading a file larger than 2GB into memory in Java

后端 未结 4 1381
南方客
南方客 2021-01-12 08:54

Since ByteArrayInputStream is limited to 2GB, is there any alternate solution that allows me to store the whole contents of a 2.3GB (and possibly larger) file i

4条回答
  •  忘掉有多难
    2021-01-12 09:41

    The whole point of StAX2 is that you do not need to read the file in to memory. You can just supply the source, and let the StAX StreamReader pull the data as it needs to.

    What additional constraints do you have that you are not showing in your question?

    If you have lots of memory, and you want to get good performance, just wrap your InputStream with a large byte buffer, and let the buffer do the buffering for you:

    // 4 meg buffer on the stream
    InputStream buffered = new BufferedInputStream(schemaInputStream, 1024 * 1024 * 4);
    

    An alternative to solving this in Java is to create a RAMDisk, and to store the file on that, which would remove the problem from Java, where your basic limitation is that you can only have just less than Integer.MAX_VALUE values in a single array.

提交回复
热议问题