You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java拆分大CSV文件时如何将表头写入所有拆分后的CSV文件

CSV拆分所有文件带表头的代码修改方案

核心修改逻辑

  • 提前读取CSV的第一行作为表头单独存储
  • 每个拆分文件创建完成后,先写入表头再写入对应批次的数据
  • 调整文件总数的计算逻辑,排除表头行避免计数偏差

调整后的完整代码

import java.io.*;
import java.util.Scanner;

public class split {

    public static void main(String args[]) {
        try {
            String inputfile = "E:/Sumit/csv-splitting-2/Proposal_Details__c.csv";
            System.out.println("输入文件路径: " + inputfile);
            
            // 每个文件包含的业务数据行数(不含表头,如果需要每个文件总共有10万行,此处填99999即可)
            int dataRowsPerFile = 99999;
            
            File file = new File(inputfile);
            Scanner scanner = new Scanner(file);
            
            int totalLines = 0;
            while (scanner.hasNextLine()) {
                scanner.nextLine();
                totalLines++;
            }
            scanner.close();
            System.out.println("文件总行数(含表头): " + totalLines);
            // 实际业务数据行数 = 总行数减1(排除表头)
            int totalDataRows = totalLines - 1;
            // 向上取整计算需要生成的文件总数
            int nof = (int) Math.ceil((double) totalDataRows / dataRowsPerFile);
            System.out.println("待生成拆分文件总数: " + nof);

            FileInputStream fstream = new FileInputStream(inputfile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            // 提前读取表头单独存储
            String header = br.readLine();
            String strLine;

            for (int j = 1; j <= nof; j++) {
                String outputpath = "E:/Sumit/csv-splitting-2/";
                String outputfile = "File-2-Proposal_Details__c" + j + ".csv";

                System.out.println("正在生成: " + outputpath + outputfile);
                FileWriter fstream1 = new FileWriter(outputpath + outputfile);
                BufferedWriter out = new BufferedWriter(fstream1);
                
                // 每个拆分文件先写入表头
                out.write(header);
                out.newLine();
                
                // 写入当前批次的业务数据行
                for (int i = 1; i <= dataRowsPerFile; i++) {
                    strLine = br.readLine();
                    if (strLine != null) {
                        out.write(strLine);
                        // 非末尾行添加换行符
                        if (i != dataRowsPerFile) {
                            out.newLine();
                        }
                    } else {
                        break;
                    }
                }
                out.close();
            }
            br.close();
            fstream.close();
        } catch (Exception e) {
            System.err.println("运行错误: " + e.getMessage());
        }
    }
}

主要修改点说明

  • 新增header变量存储表头,在遍历拆分文件前提前读取
  • 每个拆分文件初始化后优先写入表头,再写入对应批次的业务数据
  • 优化文件总数计算逻辑,直接用Math.ceil向上取整,不需要额外判断整数场景
  • 拆分表头和业务数据的行数统计,避免出现第一个文件数据行数比其他文件少一行的问题
  • 删除了原代码中无用的POI相关导入(原需求是处理CSV,不需要Excel处理依赖)
  • 补充了完整的流关闭逻辑,避免资源泄漏

内容的提问来源于stack exchange,提问作者user7616778

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 23:06:10