java 读取一个巨大的文本文件既能保证内存不溢出又能保证性能
2010-09-25 11:18:50| 分类: 默认分类 |字号 订阅
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class ReadBig {
public static String fff = "C:\\mq\\read\\from.xml";
public static void main1(String[] args) throws Exception {
final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M
File f = new File(fff);
/**
*
* map(FileChannel.MapMode mode,long position, long size)
*
* mode - 根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 FileChannel.MapMode 类中所定义的
* READ_ONLY、READ_WRITE 或 PRIVATE 之一
*
* position - 文件中的位置,映射区域从此位置开始;必须为非负数
*
* size - 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE
*
* 所以若想读取文件后半部分内容,如例子所写;若想读取文本后1/8内容,需要这样写map(FileChannel.MapMode.READ_ONLY,
* f.length()*7/8,f.length()/8)
*
* 想读取文件所有内容,需要这样写map(FileChannel.MapMode.READ_ONLY, 0,f.length())
*