zoukankan      html  css  js  c++  java
  • Java读取大文件

    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 = "D:\report\inbound\Citi_OrderLines.csv"; 
     
        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())
             */ 
     
            MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 
                    f.length() / 2, f.length() / 2); 
     
            byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容 
     
            long start = System.currentTimeMillis(); 
     
            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; 
     
                System.out.println(new String(dst, 0, length));// new 
                // String(dst,0,length)这样可以取出缓存保存的字符串,可以对其进行操作 
     
            } 
     
            long end = System.currentTimeMillis(); 
     
            System.out.println("读取文件文件一半内容花费:" + (end - start) + "毫秒"); 
     
        } 
    }

  • 相关阅读:
    滤波器组概念
    镜像滤波器
    fdatool 设计IIR滤波器
    虚拟立体环绕声
    浊音经验阈值和条件
    使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置
    投资大师索罗斯的人生轨迹 索氏投资理论 打跨英格兰银行的人 “魔鬼”索罗斯
    互联网究竟带来中心化还是去中心化 那些赚钱的模式 探讨下互联网会把社会改造成什么样子,到底是中心化还是去中心化
    cmd.exe_参数_启动参数 cmd加启动运行参数 命令
    O'Reilly总裁提姆-奥莱理:什么是Web 2.0
  • 原文地址:https://www.cnblogs.com/wasp520/p/3671897.html
Copyright © 2011-2022 走看看