Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

411
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda