How to find error lines in a CSV file upload?

Asked

Viewed 20 times

1

I am implementing a CSV file upload, currently I am able to count how many lines has the CSV, I am able to identify when errors exist in the CSV as shown in the algorithm below;

    File arquivoLeitura = new File(getArquivo());
    LineNumberReader linhaLeitura = new LineNumberReader(new FileReader(arquivoLeitura));
    linhaLeitura.skip(arquivoLeitura.length());
    int qtdLinha  = linhaLeitura.getLineNumber() + 1;


    BufferedReader leitor = new BufferedReader(new InputStreamReader(new FileInputStream(getArquivo())));

    String linha = null;

    int indiciosComErros = 0;

    for (int i = 1; i  <= qtdLinha;  i++  ) {

            linha = leitor.readLine();
            String[] dadosCSV = linha.split(VIRGULA);
            System.out.println(Arrays.toString(dadosCSV));
            System.out.println( dadosCSV[0]);
            System.out.println( dadosCSV[1]);

            System.out.println( dadosCSV[2]);
            System.out.println( dadosCSV[3]);
            System.out.println( dadosCSV[4]);
            System.out.println(dadosCSV[5]);
            System.out.println("--------------------------");

            if (pessoaJuridicaPublicaService.getPorId(Long.parseLong(dadosCSV[1])) == null) {
                indiciosComErros = indiciosComErros + 1;
            }



    }
        leitor.close();

Now I need to know which lines are wrong, example;

If you have 50 records and you have an error on line 1 and another on line 20, they will be identified. If you have 50 records and you have an error on line 10 and another on line 15 and another on line 30 and another on line 50 they are identified.

I need help because I have no idea how to accomplish this implementation!

1 answer

1


Oops, I would have a class for guards the error line data along with the line number:

class ErroCSV {
    private int linhaErro;
    private String[] dados;

    public ErroCSV(int linhaErro, String[] dados) {
        this.linhaErro = linhaErro;
        this.dados = dados;
    }

    public int getLinhaErro() {
        return linhaErro;
    }

    public String[] getDados() {
        return dados;
    }
}

And in your code:

    List<ErroCSV> erroCSVList = new Arraylist<>();

    for (int i = 1; i  <= qtdLinha;  i++  ) {

        linha = leitor.readLine();
        String[] dadosCSV = linha.split(VIRGULA);
        System.out.println(Arrays.toString(dadosCSV));
        System.out.println( dadosCSV[0]);
        System.out.println( dadosCSV[1]);

        System.out.println( dadosCSV[2]);
        System.out.println( dadosCSV[3]);
        System.out.println( dadosCSV[4]);
        System.out.println(dadosCSV[5]);
        System.out.println("--------------------------");

        if (pessoaJuridicaPublicaService.getPorId(Long.parseLong(dadosCSV[1])) == null) {
            erroCSVList.add(new ErroCSV(i, dadosCSV));
        }



    }

With erroCSVList.size() Voce you have the number of errors and in each item, the line and the data of the line with error.:

  • Thank you very much, thank you very much :)

Browser other questions tagged

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