Reading a file larger than 2GB into memory in Java

后端 未结 4 1342
南方客
南方客 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:40

    You can use memory writing the data compressed to a

    ByteArrayOutputStream baos = new ByteArrayOutputStream
    ... new GZIPOutputStream(baos));
    
    byte[] bytes = baos.toByteArray(); // < 100 MB?
    
    ByteArrayInputStream ....
    

    And then later wrap the input stream in a GZIPInputStream.

    Still a minor slow down, but should be ideal for XML.

提交回复
热议问题