繁体   English   中英

如何在所有拆分的 CSV 文件中存储 Header?

[英]How to store Header in all splitted CSV files?

下面的java代码是将一个big.csv文件拆分成多个.csv文件。 但是如何将 Header 存储在所有拆分的文件中?

import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class split {

    public static void main(String args[]) {
        try {
            String inputfile = "E:/Sumit/csv-splitting-2/Proposal_Details__c.csv";
            System.out.println("Input Path is :- " + inputfile);
            
            double nol = 100000.0;
            
            File file = new File(inputfile);
            Scanner scanner = new Scanner(file);
            
            int count = 0;
            while (scanner.hasNextLine()) {
                scanner.nextLine();
                count++;
            }
            System.out.println("Lines in the file: " + count);

            double temp = (count / nol);
            int temp1 = (int) temp;
            int nof = 0;
            if (temp1 == temp) {
                nof = temp1;
            } else {
                nof = temp1 + 1;
            }
            System.out.println("No. of files to be generated :" + nof);

            FileInputStream fstream = new FileInputStream(inputfile);
            DataInputStream in = new DataInputStream(fstream);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            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);
                for (int i = 1; i <= nol; i++) {
                    strLine = br.readLine();
                    if (strLine != null) {
                        out.write(strLine);
                        if (i != nol) {
                            out.newLine();
                        }
                    }
                }
                out.close();
            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
} 

假设你的第一行是 header,你可以有一个String header; 这将读取第一行,例如: header = br.readLine(); .

在你的 for 循环 for nof (我假设它的意思是number_of_files ),你总是在创建新文件时添加 header 作为第一行。


它会是这样的:

  • 在你的for循环之前,你只需将 header 保存在一个变量上
    • String header = br.readLine();
  • 您有 2 个 for 循环,一个用于创建文件,另一个用于将每一行写入新创建的文件
    • 在第一个 for 循环中,在您创建文件之后,您只需将 header 写入其中: our.write(header);

一般提示:

  • 使用有意义的变量名。 nolnofj ...它们都没有意义,例如,您几乎可以称它们为numOfLinesnumOfFilescurrentFile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM