zoukankan      html  css  js  c++  java
  • java中利用RandomAccessFile读取超大文件

    超大文件我们使用普通的文件读取方式都很慢很卡,在java中为我提供了RandomAccessFile函数,可以快速的读取超大文件并且不会感觉到卡哦,下面看我的一个演示实例。

    服务器的日志文件往往达到400多M,简单的文件读取实在太慢,太占用机器资源。

    特别是如果你需要5分钟就扫描一次日志文件,来统计一些即时数据。比如刚才10分钟的来访客户(大型网站用户统计系统例如51.la 会经常用到吧。)即时扫描大数据文件中的一部分显得非常之重要。
    本文讲述了如果使用java的RandomAccessFile方法从一个很大的文件来读取部分字节

    测试文件总大小46085个字节

    读取文件最后85个字节

    文件大小:46085
    85

    测试效果

    扩展功能 —> 优酷视频 
    其他的如奇艺,土豆之类操作顺序相同。当然我们也可以读取从46000只读取20个字节,看个人需要,这里仅仅作为示例

    package com.javaer.examples.file;
     
    import java.io.IOException;
    import java.io.RandomAccessFile;
     
    public class ReadBigFile {
     public static void readBigFile() throws IOException{
      String fileName = "/Users/mc2/Desktop/youku.txt";
      RandomAccessFile randomFile = null;
      randomFile = new RandomAccessFile(fileName, "r");
            long fileLength = randomFile.length();
            System.out.println("文件大小:" + fileLength);
            int start = 46000;
            randomFile.seek(start);
            byte[] bytes = new byte[91];
            int byteread = 0;
            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
            // 将一次读取的字节数赋给byteread
            while ((byteread = randomFile.read(bytes)) != -1) {
               // System.out.write(bytes, 0, byteread);
            }
            System.out.println(bytes.length);
            System.out.println(new String(bytes,"UTF-8"));
            if (randomFile != null) {
             randomFile.close();
            }
     
     }
     /**
      * @param args
      * @throws IOException 
      */
     public static void main(String[] args) throws IOException {
       ReadBigFile.readBigFile();
     }
     
    }

    即使很大的文件,从里面读取一点数据,速度也很快。全部读取出来,也会占用很少的内存。

    核心提示: randomFile.seek(start);

    跳跃读取,从这里开始读。指针直接指到start这个位置开始读取文件。

    bytes获取可以作如下替换,不同场合,不同使用

    byte[] bytes = new byte[91];
            int byteread = 0;
            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
            // 将一次读取的字节数赋给byteread
            while ((byteread = randomFile.read(bytes)) != -1) {
               // System.out.write(bytes, 0, byteread);
            }
            System.out.println(bytes.length);byte[] bytes ;
            int byteread = 0;
     
            ByteArrayOutputStream byteout = new ByteArrayOutputStream();
          byte tmp[] = new byte[1024];
          byte context[];
          int i = 0;
          int has=0;
          while ((i = randomFile.read(tmp)) != -1) {
         byteout.write(tmp, 0, i);
         has +=i;
         if(has > 10240)
           break;
         }
        bytes = byteout.toByteArray();
  • 相关阅读:
    第二十二节:类与对象后期静态绑定对象和引用
    WePHP的表单令牌验证
    第二十一节:类与对象对象的比较类型约束
    Windows下 C++ 实现匿名管道的读写操作
    Mongoose 利用实现HTTP服务
    C++ Qt 框架静态编译 操作记录
    使用Qt框架开发http服务器问题的记录
    把CMD下的color 方案遍历一遍
    C++学习笔记
    在1~10的整数范围随机5个不重复的整数
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5660541.html
Copyright © 2011-2022 走看看