Get each line of a CSV file

1

I need to get data from a CSV file and store it in an array.

Example:

CAIO ; 0909;abacaxi
BRUNO;1231;maça

I have to take for example line 1, and each value of each column store in a variable to play in a theft function (Selenium).

The way I managed to break csv, when I store for example usuarioDado[0] in a variable and printo, I show all columns, however I wanted to get the first row of that column to store in a variable, then same thing with column 2, then in the end it would go to another line.

Finally, I need to store each value of the columns of the first row in an independent variable, and then do the same thing for row 2.

That's what I did:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class capturaDados {

    private static final String VIRGULA = ";";
    private static BufferedReader reader;


     public static void main(String[] args) throws Exception {

         try{
             String arquivo = "C:\Users\rioscai\Documents\AcessosBKL.csv";
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(arquivo)));
            String linha = null;
            while ((linha = reader.readLine()) != null) {
                String[] dadosUsuario = linha.split(VIRGULA);
                System.out.println(Arrays.toString(dadosUsuario));


                // System.out.println("Ambiente: " + dadosUsuario[0]);


                /* System.out.println("Grupo: " + dadosUsuario[1]);
                System.out.println("App: " + dadosUsuario[2]);
                System.out.println("OBS: " + dadosUsuario[3]);
                System.out.println("Perfil: " + dadosUsuario[4]);
                System.out.println("--------------------------");*/


                }
             }catch(Exception e ){  
                 e.printStackTrace();

             }
     }
}

Can anyone help?

    
asked by anonymous 08.08.2018 / 20:26

1 answer

1

Caius, I believe what you want to do a sort of array with row and column. So:

  • line 1
    • column 1: CAIO
    • column 2: 0909
    • column 3: pineapple
  • line 2
    • column 1: BRUNO
    • column 2: 1231
    • column 3: apple

And so on. Correct?

So, I believe you can use two arrays, one for each row and one for each column, and merge the data into a single ArrayList. All this using only the split method.

First, break your CSV at line break (\ n) to get an array of rows.

String[] linhas = csv.split("\n");

Now, loop through the lines by breaking the semicolon (;).

String[] colunas = linha.split(";");

Every time you break the columns, add to an arrayList (which will grow automatically).

The whole code looks like this:

import java.util.ArrayList;
import java.util.List;

public class SepararCSVStackOverflow {
    public static void main(String[] args) {
        // sua lista final será esta variável
        List<String[]> lista = new ArrayList<String[]>();

        // esse é o conteúdo do seu CSV
        String seuCSV = "CAIO ; 0909;abacaxi\n"
                + "BRUNO;1231;maça";

        // obtendo cada linha do seu CSV
        String[] linhas = seuCSV.split("\n");

        // passando cada linha do seu CSV para gerar as colunas
        for (int i = 0; i < linhas.length; i++) {
            // gerando as colunas
            String[] colunas = linhas[i].split(";");

            // e adicionando à lista
            lista.add(colunas);
        }

        // mostrando, por exemplo, abacaxi que está na linha 0 (1a linha) e coluna 2 (3a coluna)  
        System.out.println(lista.get(0)[2]);
    }
}
    
08.08.2018 / 22:53