Bellow is my java code. Which is for splitting of .csv files with header with a fixed number of rows.
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 csv_split_with_hdr {
public static void main(String args[]) {
try{
String inputfile = "C:/SUMIT/backup-old/SFDC/policy-extract/temp1/xxx.csv";
System.out.println("Input Path is :- "+ inputfile);
double nol = 2800.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;
String header;
header = br.readLine();
for (int j=1;j<=nof;j++)
{
String outputpath = "C:/SUMIT/backup-old/SFDC/policy-extract/temp1";
String outputfile = "xxx"+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<=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());
}
}
}
This code compiled and executed successfully when my system configuration was as follows
Processor : intel CORE i3, OS :- windows 7
java version :- "1.8.0_202"
java(TM) SE RUNTIME Environment (build 1.8.0_202-b08)
java Hotspot(TM) client VM (build 25.202-b08, mixed mode)
But recently I chanced my system, and the system configuration is presently
Processor : intel CORE 13, OS :- Windows 11
java version :- "22.0.1" 2024-04-16
java(TM) SE RUNTIME Environment (build 22.0.1+8-16)
java Hotspot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
But Now it is giving following error at the time of compilation through the command from cmd
C:sumitbackup-oldSFDCpolicy-extracttemp1>javac csv_split_with_hdr.java
csv_split_with_hdr.java:7: error: package org.apache.poi.ss.usermodel does not exist import org.apache.poi.ss.usermodel.Cell;
^
csv_split_with_hdr.java:8: error: package org.apache.poi.ss.usermodel does not exist import org.apache.poi.ss.usermodel.Row;
^
csv_split_with_hdr.java:9: error: package org.apache.poi.xssf.usermodel does not exist import org.apache.poi.xssf.usermodel.XSSFSheet;
^
csv_split_with_hdr.java:10: error: package org.apache.poi.xssf.usermodel does not exist import org.apache.poi.xssf.usermodel.XSSFWorkbook;
^
4 errors
C:sumitbackup-oldSFDCpolicy-extracttemp1>