• java 读取txt,java读取大文件


    java 读取txt,java读取大文件

    package com.bbcmart.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 = 0x1200000;// 缓冲大小为12M
           
            System.out.println(BUFFER_SIZE);

            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];// 每次读出12M的内容
                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) + "毫秒");
        }
    }

  • 相关阅读:
    luogu P5325 Min_25筛
    P5468 [NOI2019]回家路线 斜率优化 dp
    退役了
    6.18 省选模拟赛 树 倍增 LCT
    导出excel时一个页面问题-X11GraphicsEnvironment
    2019首篇
    Glide:重新加载失败的问题
    Android上传图片的两种方式
    Bug:No mapping for GET /onepill//swagger-ui.html
    Android集成百度地图
  • 原文地址:https://www.cnblogs.com/firstdream/p/5535990.html
Copyright © 2020-2023  润新知