使用Java分割大文件

一、前言

在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 为文件名称,可以为全路径,也可以为当前目录的路径 必填

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值