I want to split a 1 million row CSV file into smaller chunk files with 10000 each. I need to store header once and then write into all the split files. Basically for example if number of lines to be split are 3 then writing to split files should happen with 3 lines + header.
public static void splitCSV(final String fileName,
final String extension,
final int maxLines) {
try (Scanner s = new Scanner(new FileReader(String.format("%s.%s", fileName, extension)))) {
int file = 0;
int cnt = 0;
BufferedWriter writer = new BufferedWriter(new FileWriter(String.format("%s_%d.%s", fileName, file, extension)));
while (s.hasNext()) {
writer.write(s.next() + System.lineSeparator());
if (++cnt == maxLines && s.hasNext()) {
writer.close();
writer = new BufferedWriter(new FileWriter(String.format("%s_%d.%s", fileName, ++file, extension)));
cnt = 0;
}
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}}