• java读取 500M 以上文件,java读取大文件


    设置缓存大小BUFFER_SIZE ,Config.tempdatafile是文件地址

    package com.yjf.util;
    import java.io.File;
    import java.io.RandomAccessFile;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;

    public class Test {
    public static void main(String[] args) throws Exception {
    final int BUFFER_SIZE = 0x300000; // 缓冲区为3M
    File f = new File(Config.tempdatafile);
    int len = 0;
    Long start = System.currentTimeMillis();
    for (int z = 8; z >0; z--) {
    MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
    .getChannel().map(FileChannel.MapMode.READ_ONLY,
    f.length() * (z-1) / 8, f.length() * 1 / 8);
    byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
    for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
    if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
    for (int i = 0; i < BUFFER_SIZE; i++)
    dst[i] = inputBuffer.get(offset + i);
    } else {
    for (int i = 0; i < inputBuffer.capacity() - offset; i++)
    dst[i] = inputBuffer.get(offset + i);
    }
    int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
    : inputBuffer.capacity() % BUFFER_SIZE;
    len += new String(dst, 0, length).length();
    System.out.println(new String(dst, 0, length).length()+"-"+(z-1)+"-"+(8-z+1));
    }
    }
    System.out.println(len);
    long end = System.currentTimeMillis();
    System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
    }
    }

    来源博客http://yijianfengvip.blog.163.com/blog/static/175273432201191354043148/


  • 相关阅读:
    树莓派ZeroW的Python中使用SQLite数据库
    树莓派Python读写配置文件--configparser库
    信号量示例——“生产者——消费者”实验
    互斥锁示例——模拟银行卡取钱
    管道通信(上)
    命名管道——进程通信案例
    文件I/O
    链表习题
    蓝桥杯ACM训练Day4——算法2-8~2-11:链表的基本操作
    C++——类模板几种常见的情况
  • 原文地址:https://www.cnblogs.com/shanzei/p/2413514.html
Copyright © 2020-2023  润新知