Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

416
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda