I am trying read and write large files (larger than 100 MBs) using BufferedInputStream
& BufferedOutputStream
. I am getting Heap Memory issue & OOM exception.
The code looks like :
BufferedInputStream buffIn = new BufferedInputStream(iStream);
/** iStream is the InputStream object **/
BufferedOutputStream buffOut=new BufferedOutputStream(new FileOutputStream(file));
byte []arr = new byte [1024 * 1024];
int available = -1;
while((available = buffIn.read(arr)) > 0) {
buffOut.write(arr, 0, available);
}
buffOut.flush();
buffOut.close();
My question is when we use the BufferedOutputStreeam
is it holding the memory till the full file is written out ?
What is the best way to write large files using BufferedOutputStream
?
Create a BufferedOutputStream BufferedOutputStream package first. Once we import the package here is how we can create the output stream. // Creates a FileOutputStream FileOutputStream file = new FileOutputStream(String path); // Creates a BufferedOutputStream BufferedOutputStream buffer = new BufferOutputStream(file);
BufferedOutputStream(OutputStream out) Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
there is nothing wrong with the code you have provided. your memory issues must lie elsewhere. the buffered streams have a fixed memory usage limit.
the easiest way to determine what has caused an OOME, of course, is to have the OOME generate a heap dump and then examine that heap dump in a memory profiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With