Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
How to create multiple csv file from large csv file in Java

I am kind of stuck, I usually know how to create single csv, it looks like I am missing or disconnecting from this code. I am not able to create multiple csv file from Pojo class. The file usually is more than 15mb, but I need to split into multiple csv file like 5mb each. Any suggestion would be great helped. Here is sample code that I am trying but failing.

    public static void main(String[] args) throws IOException {
        getOrderList();
    }

    public static void getOrderList() throws IOException {
        List<Orders> ordersList = new ArrayList<>();
        Orders orders = new Orders();
        orders.setOrderNumber("1");
        orders.setProductName("mickey");

        Orders orders1 = new Orders();
        orders1.setOrderNumber("2");
        orders1.setProductName("mini");

        ordersList.add(orders);
        ordersList.add(orders1);
        Object [] FILE_HEADER = {"orderNumber","productName"};
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int rowCount = 0;
        int fileCount = 1;
        try {
            BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(byteArrayOutputStream));
            CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter,
                    CSVFormat.DEFAULT.withRecordSeparator("\n"));
            csvFilePrinter.printRecord(FILE_HEADER);
            for (Orders patient : ordersList) {
                rowCount++;
                patient.getOrderNumber();
                patient.getProductName();
                if (rowCount <= 1) {
                    csvFilePrinter.printRecord(patient);
                    csvFilePrinter.flush();
                }
                if (rowCount > 1 ) {
                    csvFilePrinter.printRecord(patient);
                    fileCount++;
                   csvFilePrinter.flush();
                }
            }


        } catch (IOException e) {
            throw new RuntimeException("Cannot generate csv file", e);
        }
        byte[] csvOutput = byteArrayOutputStream.toByteArray();
        OutputStream outputStream = null;
        outputStream = new FileOutputStream("demos" + fileCount + ".csv");
        byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(csvOutput);
        byteArrayOutputStream.writeTo(outputStream);
    }


    public static class Orders {
        private String orderNumber;
        private String productName;

        public String getOrderNumber() {
            return orderNumber;
        }

        public void setOrderNumber(String orderNumber) {
            this.orderNumber = orderNumber;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }
    } 
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!