Memory issues with InputStream in Java

后端 未结 4 1088
孤城傲影
孤城傲影 2021-01-21 19:31

I need to read a file into an array of Bytes.The entire file needs to be read into the array. The problem is I am getting an OutOfMemory Error since the file size is too large.

4条回答
  •  攒了一身酷
    2021-01-21 20:01

    No, if your file is too large to fit into memory, it's too large to fit in memory.

    A better solution would be to try to process the stream as a stream, rather than loading the whole thing into memory. Without knowing what processing you're trying to achieve, we can't really tell whether or not that's feasible.

    For example, if you're just trying to compute a secure hash of the file, you should be able to do that without loading significant amounts of data in at a time - but if your processing requires random access to data, you may need to use RandomAccessFile instead.

提交回复
热议问题