zoukankan      html  css  js  c++  java
  • NIO入门之轻松读取大文件

        今天同事碰到了一个问题,从游戏服务器下载下来的输出log有一个多G大。用记事本打不开,EditPlus也打不开,都提示文件太大。用word也打不开,提示文件大于512M。打不开怎么查找错误啊。于是他问我解决办法。我想了想,决定写一个简单的程序读取这个log,把这个log切分成一些小的可以用Editplus打开的文本。正好前段时间看了一些NIO的东西,所以决定用NIO来写。没想到,10几行代码就搞定了。下面附上源代码:
         ReadLargeTextWithNIO.java

     1package com.nio.entrace;
     2
     3import java.io.FileInputStream;
     4import java.io.FileOutputStream;
     5import java.io.IOException;
     6import java.nio.ByteBuffer;
     7import java.nio.channels.FileChannel;
     8
     9/**
    10 * 
    11 * 用NIO读取大文本(1G以上)
    12 * 
    13 * @author landon
    14 *
    15 */
    16public class ReadLargeTextWithNIO 
    17{
    18 public static void main(Stringargs) throws IOException
    19 {
    20  FileInputStream fin = new FileInputStream("d:\temp\outlineA1.log");
    21  FileChannel fcin = fin.getChannel();
    22  
    23  ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 50);
    24  
    25  while(true)
    26  {
    27   buffer.clear();
    28   
    29   int flag = fcin.read(buffer);
    30   
    31   if(flag == -1)
    32   {
    33    break;
    34   }
    35   
    36   buffer.flip();
    37   
    38   FileOutputStream fout = new FileOutputStream("d:\temp\" + Math.random() + ".log");
    39   FileChannel fcout = fout.getChannel();
    40   
    41   fcout.write(buffer);
    42  }
    43 }
    44}
    45
    46


        下面简单说几个注意的地方:
         a.因为要把超大文本切分成小的部分,所以分配buffer的时候尽量大一些,这里我分配的大小是50M,不过如果太大了,可能会报内存溢出。
         b.说一下clear和flip的方法,直接上源码:

     1public final Buffer clear()
     2    {
     3        position = 0;
     4        limit = capacity;
     5        mark = -1;
     6        return this;
     7    }
     8
     9 public final Buffer flip()
    10    {
    11        limit = position;
    12        position = 0;
    13        mark = -1;
    14        return this;
    15    }


        一看便知二者的区别。

        c.跳出循环也即读完的判断是read返回的flag是-1

        利用NIO确实方便,以后继续研究->NIO网络编程

  • 相关阅读:
    前端常用代码
    前端常用代码
    Velocity模版自定义标签
    算法思想
    java特性之三--多态性
    非线性数据结构--图
    接口
    java特性之四--抽象
    (数据结构与算法) 堆
    数据结构_平衡二叉树(AVL树)
  • 原文地址:https://www.cnblogs.com/firstdream/p/5536034.html
Copyright © 2011-2022 走看看