java.lang.OutOfMemory尝试将大型PDF文件读取到字节数组时出错
我试图用字节数组读取巨大的pdf文件, 这是我的代码java.lang.OutOfMemory尝试将大型PDF文件读取到字节数组时出错,java,pdf,out-of-memory,bytearray,fileinputstream,Java,Pdf,Out Of Memory,Bytearray,Fileinputstream,我试图用字节数组读取巨大的pdf文件, 这是我的代码 String RESULT "D/new/certficateVisualFinal.pdf"; try { FileInputStream fileInputStream=null; File file = new File(RESULT); byte[] bFile = new byte[(int) file.length()]; //convert
String RESULT "D/new/certficateVisualFinal.pdf";
try {
FileInputStream fileInputStream=null;
File file = new File(RESULT);
byte[] bFile = new byte[(int) file.length()];
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
reader = new PdfReader(bFile);
pdfStamper = new PdfStamper(reader, new FileOutputStream(outPut));
pdfStamper.setOutlines(outlineVisual);
pdfStamper.setFormFlattening(true);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
但当我尝试去做的时候,我有一个失忆
fileInputStream.read(bFile);
这就是错误所在
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:220)
请帮帮我。谢谢。完全不要使用字节数组
PdfReader
有一个带有InputStream
参数的构造函数,因此您可以直接将FileInputStream
传递给该构造函数。我以前试过。这就是为什么我要尝试另一种方式。好的,我刚才读了你的另一个问题。让我们看看我是否理解正确。您分配了1GB,读取此文件时内存不足;因此,您决定尝试将整个1.5GB文件读取到1GB内存中,看看这是否会消除内存不足错误?