I am trying to read a large file (>150MB) and return the file content as a ByteArrayOutputStream
. This is my code...
private ByteArrayOutputStream readfileContent(String url) throws IOException{
log.info("Entering readfileContent ");
ByteArrayOutputStream writer=null;
FileInputStream reader=null;
try{
reader = new FileInputStream(url);
writer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = reader.read(buffer);
while (bytesRead = > -1) {
writer.write(buffer, 0, bytesRead);
buffer = new byte[1024];
}
}
finally {
writer.close();
}
log.info("Exiting readfileContent ");
return writer;
}
I am getting an java.lang.OutOfMemoryError: Java heap space exception
. I have tried increasing the java heap size, but it still happens. Could someone please assist with this problem.