Grab each row of a CSV file

Asked

Viewed 987 times

1

I need to take 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 row 1, and each value of each column store in a variable to play in a robot 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, but I wanted to take the first row of this column to store in a variable, then same thing with column 2, then at the end I would go to another row.

Anyway, I need to store each value of the first row columns 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();

             }
     }
}

Anyone can help?

  • Caio, edit your question and include the code you made. This helps anyone who helps you.

  • Tomorrow I will post , because this on the work PC

1 answer

1


Caio, I believe that whatever you want to make a kind of matrix 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. Right?

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

First, break your CSV in line break ( n) to get an array of lines.

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 goes 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]);
    }
}
  • Oops, that’s right, I’ll test tomorrow at work. But what you said is exact what I need.

  • Because I have a list of accesses in a csv ,then I made a robot that I have to type platform, environment and etc,but I was not able to catch the first line,second and etc , only the whole column

  • Thank you, it worked

Browser other questions tagged

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