使用Java将大于2GB的文件读入内存 [英] Reading a file larger than 2GB into memory in Java
本文介绍了使用Java将大于2GB的文件读入内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于 ByteArrayInputStream
限制为2GB,是否有任何替代解决方案允许我将2.3GB(可能更大)文件的全部内容存储到<$要被Stax2读取的c $ c> InputStream ?
Since ByteArrayInputStream
is limited to 2GB, is there any alternate solution that allows me to store the whole contents of a 2.3GB (and possibly larger) file into an InputStream
to be read by Stax2?
当前代码:
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(in); //ByteArrayInputStream????
try
{
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(new StreamSource(schemaInputStream));
Validator validator = schema.newValidator();
validator.validate(new StAXSource(xmlStreamReader));
}
finally
{
xmlStreamReader.close();
}
对于性能调优,变量 in
一定不要来自磁盘。我有 plenties 的RAM。
For performance tuning, variable in
must not come from disk. I have plenties of RAM.
推荐答案
使用NIO将文件读入一个巨大的ByteBuffer,并且然后创建一个读取ByteBuffer的流类。在开源中有几个这样的浮动。
Use NIO to read the file into a gigantic ByteBuffer, and then create a stream class that reads the ByteBuffer. There are several such floating around in open sources.
这篇关于使用Java将大于2GB的文件读入内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文