当前位置: 代码迷 >> J2SE >> 大文件如何读取
  详细解决方案

大文件如何读取

热度:248   发布时间:2016-04-24 17:23:58.0
大文件怎么读取
我要从一个文本文件中提有用的数据
文本文件200多MB
是不是可以建一个缓存来把有用的数据一段一段的提出来,请问该怎么做?

------解决方案--------------------
呵呵,200MB而已...
JAVA中可以使用内存映射文件来操作大文件.
最大可达2GB.
下面是个简单的示例,更具体的自己看Java API DOCS或相关资料
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class LargeMappedFiles {
static int length = 0x8FFFFFF; // 128 Mb
public static void main(String[] args) throws Exception {
MappedByteBuffer out =
new RandomAccessFile( "test.dat ", "rw ").getChannel()
.map(FileChannel.MapMode.READ_WRITE, 0, length);
for(int i = 0; i < length; i++)
out.put((byte) 'x ');
System.out.println( "Finished writing ");
for(int i = length/2; i < length/2 + 6; i++)
System.out.print((char)out.get(i)); //read file
}
} ///
  相关解决方案