使用java nio 将超过2G文件映射到内存中,报异常

代码如下:
MappedByteBuffer buffer = FileChannel.map(FileChannel.MapMode.READ_ONLY, 0, Integer.MAX_VALUE);//int java.lang.Integer.MAX_VALUE = 2147483647 [0x7fffffff]

环境:jdk7 64位 操作系统系统win7 64位

异常信息
Exception in thread "main" java.io.IOException: Map failed
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:888)
at com.bigfile.read.MappedBiggerFileReader.(MappedBiggerFileReader.java:36)
at com.bigfile.read.MappedBiggerFileReader.main(MappedBiggerFileReader.java:83)
Caused by: java.lang.OutOfMemoryError: Map failed
at sun.nio.ch.FileChannelImpl.map0(Native Method)
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:885)
... 2 more

out of memory了,内存不够,map失败

Caused by: java.lang.OutOfMemoryError: Map failed,你这也太狠了吧2G,直接放在内存里面的东西一般是数量少,使用频繁,不怎么需要修改的数据。
所以你这个设计就不怎么合理了,你这里最好把文件拆分为多个文件,分批读入数据库,之后把数据作为普通表中数据处理,或者你能用行读取,每次读取一定的 行数,之后存入数据库,再继续从刚刚读取的位置重新读取。

需要为了快速的读取文件分析文件,所以使用映射内存文件的方式