Store CSV file data in a list

Asked

Viewed 23 times

0

I am reading a 9 million-line CSV file, and I need to turn each line into an object and store it in a list or buffer to subsequently write to a random access file.

When I try to add the investor objects in the list gives the following error when it reaches 6.462.547 million lines: inserir a descrição da imagem aqui

How can I get around this mistake?

Link to the file I’m reading

My reading function (OBS.: The Textfile class can be replaced by the Bufferedreader):

public List<Object> lerDadosCSV(String arquivoCSV, JProgressBar progressBar, JTextField textField, int tipo) {
    long indice = 0;
    numeroTotalLinhas = numeroTotalLinhas(arquivoCSV) * 2;
    try (TextFile textFile = new TextFile(arquivoCSV)) {
        DecimalFormat decimalFormat = new DecimalFormat("#,###");
        String linha;
        List<Object> records = new ArrayList<>();
        while ((linha = textFile.readLine()) != null) {
            if (indice != 0) {
                records.add(tipo == 0 ? montaEstoque(linha.split(";")) : montaInvestidor(linha.split(";")));
            }
            textField.setText(decimalFormat.format(indice));
            progressBar.setValue((int) (indice * 100 / numeroTotalLinhas));
            progressBar.setString((int) (indice * 100 / numeroTotalLinhas) + "%");
            indice++;
        }
        return records;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Function montage:

    public static Investidor montaInvestidor(String[] splitLinha) {
        try {
            boolean operou = (splitLinha[10].contains("s|S")) ? true : false,
                    situacao = (splitLinha[9].contains("a|A")) ? true : false;
            Investidor investidor = new Investidor(Integer.parseInt(splitLinha[0]), formataData.parse(splitLinha[1]),
                    splitLinha[2].trim(), splitLinha[3].trim(), splitLinha[4].trim(), splitLinha[6].trim(),
                    splitLinha[7].trim(), splitLinha[8].trim(), Integer.parseInt(splitLinha[5]), situacao, operou);
            return investidor;
        } catch (NumberFormatException | ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

1 answer

0

Good night, Matheus, good night! Your program is giving a bang on the stack(stackoverflow hahaha), this using more memory than the application has available, if you are using Tomcat think it is not your case, can increase manually, and can tbm optimize the code, do less things within the function, for example only create the objects and then format them

  • I don’t use any framework in this project

Browser other questions tagged

You are not signed in. Login or sign up in order to post.