文件拆分为10份

package com.csci.ai.office;

import java.io.*;

public class FileSplitter {

    public static void main(String[] args) {
        // 定义源文件路径和要拆分的份数
        String filePath = "/Users/ivan/code/AZ/ai-co-office-helper/src/main/resources/docs/AAA.txt";
        int numSplits = 11;

        try {
            System.out.println("开始拆分文件...");
            splitFileByLines(filePath, numSplits);
            System.out.println("文件拆分完成。");
        } catch (IOException e) {
            System.err.println("文件拆分过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 将文件按行数平均拆分成多个小文件
     *
     * @param filePath  源文件路径
     * @param numSplits 要拆分的份数
     * @throws IOException 如果读写文件时发生错误
     */
    public static void splitFileByLines(String filePath, int numSplits) throws IOException {
        File inputFile = new File(filePath);
        if (!inputFile.exists()) {
            throw new FileNotFoundException("源文件未找到: " + filePath);
        }

        long totalLines = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
            while (reader.readLine() != null) totalLines++;
        }

        if (totalLines == 0) {
            System.out.println("源文件为空,无需拆分。");
            return;
        }

        long linesPerSplit = (long) Math.ceil((double) totalLines / numSplits);

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
            String line;
            int fileCount = 0;
            int lineCount = 0;
            long charCount = 0;
            BufferedWriter writer = null;
            String outputFilePath = "";

            String fileBaseName = inputFile.getName().substring(0, inputFile.getName().lastIndexOf('.'));
            String fileExtension = inputFile.getName().substring(inputFile.getName().lastIndexOf('.'));

            while ((line = reader.readLine()) != null) {
                if (lineCount % linesPerSplit == 0) {
                    if (writer != null) {
                        writer.close();
                        System.out.printf("文件 '%s' 创建成功,包含 %d 行, %d 个字符。%n", new File(outputFilePath).getName(), lineCount, charCount);
                    }
                    if (fileCount < numSplits) {
                        fileCount++;
                        outputFilePath = String.format("%s/%s-%d%s", inputFile.getParent(), fileBaseName, fileCount, fileExtension);
                        writer = new BufferedWriter(new FileWriter(outputFilePath));
                        lineCount = 0;
                        charCount = 0;
                    }
                }
                if (writer != null) {
                    writer.write(line);
                    writer.newLine();
                    lineCount++;
                    charCount += line.length();
                }
            }

            if (writer != null) {
                writer.close();
                System.out.printf("文件 '%s' 创建成功,包含 %d 行, %d 个字符。%n", new File(outputFilePath).getName(), lineCount, charCount);
            }
        }
    }
}
posted @ 2025-07-28 09:17  iiiiiiiivan  阅读(9)  评论(0)    收藏  举报