Use java.io.RandomAccessFile class to implement large text log cutting

Analyzing some background logs is a good way to locate the problem, but some logs are large, and it is slow to open with the problem editor, and it is also slow to read the logs alone. .
code show as below:

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

Run it successfully!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584700&siteId=291194637