java读取大文件的最优方法

/**
 * 读文件并且写入另一个文件
 */
public static void readAndWrite() throws Exception {
    //创建一个随机读写文件对象
    RandomAccessFile raf=new RandomAccessFile("out.txt","rw");
    long totalLen=raf.length();
    FileInputStream inputStream = new FileInputStream("out.txt");
    FileOutputStream outputStream = new FileOutputStream("out2.txt");
    FileChannel inputChannel = inputStream.getChannel();
    FileChannel outputChannel = outputStream.getChannel();
    MappedByteBuffer allocate /*= ByteBuffer.allocate(2)*/;
    //size大小决定文件缓冲buffer大小,手动细化操作buffer时需注意,减少OOM
    allocate=raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 100);
    while (true) {
        allocate.clear(); // 每次读取完毕后都要 清空 buffer,
        // 如果不清空buffer inputChannel.read(allocate)永远不会=-1 不会跳出循环,读取到文件的数据也会重复
        int read = inputChannel.read(allocate);
        if (read == -1) {
            break;
        }
        int i=0;
    for (byte b:"123".getBytes()) {
        allocate.put(90+i, b);
        i++;
    }
        //实现真实c场景的文件读写,支持断电文件保护,减少OOM
        allocate.force();
        //适配转换数据与文件的格式,减少乱码和写不进去的情况
        allocate.flip();
        outputChannel.write(allocate);
    }
}
/**
 * 写文件
 *
 * @throws Exception
 */
public static void writeFile() throws Exception {
    FileOutputStream outputStream = new FileOutputStream("out.txt");
    FileChannel channel = outputStream.getChannel();
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(50);
    byteBuffer.put("hello,nio".getBytes());
    byteBuffer.flip();
    while (byteBuffer.hasRemaining()) {
        channel.write(byteBuffer);
    }
}

/**
 * 读取文件
 *
 * @throws Exception
 */
public static void readFile() throws Exception {
    FileInputStream inputStream = new FileInputStream("out.txt");
    FileChannel channel = inputStream.getChannel();
    ByteBuffer byteBuffer = ByteBuffer.allocate(20);
    while (true) {
        int read = channel.read(byteBuffer);
        if (-1 == read) {
            break;
        }
    }
    byteBuffer.flip();
    System.out.println(byteBuffer.position() + "," + byteBuffer.limit() + "," + byteBuffer.capacity());
    //方法一
    //System.out.println(new String(byteBuffer.array()));
    /*while (byteBuffer.hasRemaining()){
    }/
    // 方法二
    /*while (channel.read(byteBuffer)!=-1){
        byte[] array = byteBuffer.array();
        System.out.println(new String(array));
        byteBuffer.clear();
    }*/

    //方法三
    int i = 0;
    byte[] bytes = new byte[byteBuffer.capacity()];
    while (byteBuffer.hasRemaining()) {
        bytes[i] = byteBuffer.get();
        i++;
    }
    System.out.println(new String(bytes));
}
RandomAccessFile优点
1、可以直接跳到文件的任意位置来读写数据
2、允许自由定位文件记录指针,可以不从文件开始的地方进行输出
3、记录指针的方法:
    long getFilePointer(); 返回文件记录指针的当前位置
    void seek(long pos); 将文件记录指针定位到pos位置
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swordbob

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值