一、前言

在window系统中有一个大文件需要查看,但是没有像Linux那样的工具命令,所以自己写了一个,目前只能按行切割

二、代码

https://gitee.com/belongQK/split-fileicon-default.png?t=M85Bhttps://gitee.com/belongQK/split-file

package com.qiankun;

import java.io.*;

/**
 * @Auther: qiankun
 * @Date: 2022/10/24 15:36
 * @Description :
 **/
public class SplitFile {
    private static String SPLIT_TYPE = "l";// 默认按行分割 分割类型,l:为按行分割,s: 为按大小分割 (未支持)
    private static int SIZE = 100000; //默认10000行 分割大小 为10000 行
    private static String FILE_PATH = "";
    private static int fileCount = 1;

    public static void main(String[] args) throws FileNotFoundException {
        // 获取设置参数
//        if (args.length % 2 != 0) {
//            return;
//        }
        for (int i = 0; i < args.length; i++) {
            if (args[i].equalsIgnoreCase("-t")) {
                SPLIT_TYPE = args[++i];
            }
            if (args[i].equalsIgnoreCase("-s")) {
                SIZE = Integer.parseInt(args[++i]);
            }
            if (args[i].equalsIgnoreCase("-f")) {
                FILE_PATH = args[++i];
            }
        }
        if (!checkFilePath()) {
            return;
        }
        splitFileAccordingToTheNumberOfLines();
    }

    /**
     * 检查文件是否存在 路径是否正确
     *
     * @return
     */
    private static boolean checkFilePath() {
        String property = System.getProperty("user.dir");
        if (FILE_PATH == null || FILE_PATH.length() == 0) {
            System.out.println("文件信息有误");
            return false;
        }
        File file = new File(FILE_PATH);
        if (!file.exists()) {
            file = new File(property + "\\" + FILE_PATH);
            if (file.exists()) {
                FILE_PATH = property + "\\" + FILE_PATH;
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据行数切割文件
     */
    private static void splitFileAccordingToTheNumberOfLines() throws FileNotFoundException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_PATH)));
        BufferedWriter bufferedWriter = getBufferedWriter();
        int i = 1;
        String line = "";
        try {
            while (((line = bufferedReader.readLine()) != null)) {
                if (i >= SIZE) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    bufferedWriter = getBufferedWriter();
                    i = 1;
                }
                bufferedWriter.write(line);
                bufferedWriter.write("\n");
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            try {
                bufferedWriter.flush();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            try {
                bufferedWriter.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }


    }

    private static BufferedWriter getBufferedWriter() throws FileNotFoundException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FILE_PATH + "-" + (fileCount++) + ".log")));
    }
}

三、使用方式

打包以后

java -jar file-split.jar -f access.log-67.log -s 10000

-s 为切割行数默认100000
-f 为文件名称,可以为全路径,也可以为当前目录的路径 必填

Logo

更多推荐