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

java 读取txt,java读取大文件

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

 有2种办法。 看看你觉得哪种适合。


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  readBigFile(String filepath){
        final int BUFFER_SIZE = 0x1200000; // 缓冲区为3M
        Long len = 0l;
        String temp = "";
        Long start = System.currentTimeMillis();
        try {
            File f = new File(filepath);
            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;
                    temp = new String(dst, 0, length);
                    len += temp.length();
                    System.out.println(temp.length()+"-"+(z-1)+"-"+(8-z+1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(len);
        long end = System.currentTimeMillis();
        System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
    }
    
    public static void readBigFile2(String filepath) {
          try {
                Long start = System.currentTimeMillis();
              FileInputStream fin = new FileInputStream(filepath);
              FileChannel fcin = fin.getChannel();
              Long len = 0l;
              ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 100);
              
//              FileOutputStream fout = new FileOutputStream(Math.random() + ".log");
              while(true)
              {
               buffer.clear();
               
               int flag = fcin.read(buffer);
               
               if(flag == -1)
               {
                break;
               }
               
               buffer.flip();
               
               
//               FileChannel fcout = fout.getChannel();
               ///
               len += buffer.getInt();
//               fcout.write(buffer);
            }
                long end = System.currentTimeMillis();
                System.out.println("读取文件文件花费:" + (end - start) + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        readBigFile2("bigfile.txt");
    }

}

七七八八网 http://www.qi788.com/

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


  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值