使用java.io.RandomAccessFile类实现大型文本日志切割

分析一些后台日志是对问题定位很好的方法,但是有些日志比较大,使用问题编辑器打开也比较慢,同时一个人去看日志也比较慢,通过日志切割,可并发多人同时排查问题等。
代码如下:

package fileExtract;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Main {

    static String path = "D:\\Users\\workspace\\Test\\fileSplit\\fileExtract\\file\\";
    static void split() throws IOException {
        File sourceFile = new File(path+"log.txt");
        System.out.println(sourceFile.exists());
        int numberOfPieces = 10;        //默认文件切割的数量
        long fileLength = sourceFile.length() / numberOfPieces;        //分一下每一个小文件的大小
        byte[] b = new byte[1024];       
        RandomAccessFile raf1 = new RandomAccessFile(sourceFile, "r");
        int len = -1;
        for(int i = 0; i < numberOfPieces; i++) {
            String name = path+"log_"  + (i+1)+".txt";
            File file = new File(name);
            file.createNewFile();
            RandomAccessFile raf2 = new RandomAccessFile(file, "rw");
            while((len = raf1.read(b)) != -1) {
                raf2.write(b, 0, len);        
                if(raf2.length() > fileLength)        //如果太大了就不在这个子文件写了 换下一个
                    break;
            }
            raf2.close();
        }
        raf1.close();
    }

    static void merge() throws IOException {
        File[] files = new File[10];
        String name = "log_";
        File file = new File(path+"log.txt");
        file.createNewFile();
        RandomAccessFile in = new RandomAccessFile(file, "rw");
        in.setLength(0);
        in.seek(0);
        byte[] bytes = new byte[1024];
        int len = -1;
        for(int i = 0; i < files.length; i++) {
            files[i] = new File(path+name + (i + 1)+".txt");
            System.out.println(files[i].exists());
            RandomAccessFile out = new RandomAccessFile(files[i], "rw");
            while((len = out.read(bytes)) != -1) {
                in.write(bytes, 0, len);
            }
            out.close();
        }
        in.close();
    }

        public static void main(String[] args) throws IOException {
//            split();
            merge();
        }
    }

运行成功即可!

猜你喜欢

转载自blog.csdn.net/uniquewonderq/article/details/79961072