通过RandomAccessFile实现MyRandomAccessFile.readLine()方法--消除乱码

本文介绍了一种解决使用RandomAccessFile读取.log文件时出现乱码问题的方法,通过自行实现readLine方法来逐行读取文件内容。

这两天用RandomAccessFile的readLine读取.log文件,不管怎样处理读出来的line都是乱码,无奈之下只能自己实现一个readLine方法,先读出bytes

再判断是否换行,一行一行的读出,注意filePointer的变化。

参考:http://hi.baidu.com/nathena/item/84f33079fa457b3a71442397

package com.dhcc.logtailer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

public class MyRandomAccessFile {
    
    private RandomAccessFile file;
    private byte[] b;
    private String charSet;
    private long filePointer;
    
    public MyRandomAccessFile(File file,String mode,String charSet) throws FileNotFoundException{
        this.file = new RandomAccessFile(file,mode);
        this.charSet = charSet;
    }
    public void seek(long pos) throws IOException{        
        this.file.seek(pos);
    }
    public long getFilePointer() throws IOException{
        return this.file.getFilePointer();
    }
    public void close() throws IOException{
        this.file.close();
    }

    public String readLine() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int size = 0;
        boolean ifCRLF = false;
        int read = 0; // 已读数
        while (true) {
            // 填充byte[]
            fill();
            size = b.length;
            if(b.length==0){
                   return null;
               }
            for (int i = 0; i < size; i++) {
                read++;
                if (b[i] == '\n') {
                    ifCRLF = true;
                    break;
                }
                if (b[i] != '\r') {
                    baos.write(b[i]);
                }
            }

            // 重置b
            b = Arrays.copyOfRange(b, read, size);
            if (ifCRLF)
                break;
        }

        byte[] b = baos.toByteArray();
        file.seek(this.filePointer+read);
        baos.close();// ByteArrayOutputStream close无用。
        baos = null;
        return new String(b, charSet);
    }
    
    private void fill() throws IOException{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (b != null && b.length > 0) {
            baos.write(b, 0, b.length);
        }
        int size = 0;
        long filelength = file.length();
        long filepointer = file.getFilePointer();
        if(filepointer>=filelength){
            b = new byte[0];
            return;
        }
        byte[] _b = new byte[(int) (filelength - filepointer)];        
        this.filePointer = file.getFilePointer();
        size = file.read(_b);
        baos.write(_b, 0, size);
        _b = null;
        b = baos.toByteArray();
        baos = null;
    }
}

 

转载于:https://www.cnblogs.com/eruler/p/3368818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值