java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

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();


This entry was posted in JAVA and tagged , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-randomaccessfile/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

2 Responses to java使用RandomAccessFile快速读取超大文件

  1. hostgator says:

    具体而详细的内容,支持一下

  2. jason says:

    不错,写的很详细,感谢分享

Leave a Reply to hostgator Cancel reply