Writing Large Files Using BufferedOutputStream

I am trying to read and write large files (over 100 MB) with BufferedInputStreamand BufferedOutputStream. I get a heap memory issue and an OOM exception.
The code looks like this:

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 BufferedOutputStreeam, does it hold memory until the file is completely written out?
What is the best way to write large files using BufferedOutputStream?

+5
source share
1 answer

There is nothing wrong with the code you provided. your memory problems should lie elsewhere. buffered streams have a fixed memory limit.

, OOME, , , OOME , .

+5

All Articles