Read file format CSV in Java

1

I would like to know how I have to proceed to read a file in CSV with Java and skip a comma of separation between each field. Read each space that is found but I want it to be each comma (,) how could I do it?

This is my code:

class Pruebas {
    public static void main(String [] args) {
        String cadena = "MetroBikeShare_2016_Q3_trips.csv";
        String elemento=null;
        boolean seguir = true;
        Scanner entrada = null;

        try {
            entrada = new Scanner(new File(cadena));
        }
        catch (FileNotFoundException fnE) {
            System.out.println(fnE.getMessage());
            seguir = false;
        }
        if (seguir) {
            entrada.nextLine();
            while (entrada.hasNext()) {
                elemento=entrada.next();
                System.out.println(elemento);
            }
        }
    }
}
    
asked by gabriel gomez 07.12.2016 в 23:14
source

2 answers

2

Assuming a CSV file :

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

This is an example of how to read the contents of the CSV file:

String csvFile = "archivo.csv";
BufferedReader br = null;
String line = "";
//Se define separador ","
String cvsSplitBy = ",";
try {
    br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {                
        String[] datos = line.split(cvsSplitBy);
        //Imprime datos.
       System.out.println(datos[0] + ", " + datos[1] + ", " + datos[2] + ", " + datos[3] + ", " + datos[4] + ", " + datos[5]);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    
answered by 08.12.2016 / 00:28
source
0

I was also in that doubt, what I did was implement the method Split in Java this is an example of how to implement it, and so you can read line by line of the file and with the Split you save it in another array or arraylist as chains depends how you are going to use it

while ((line = rd.readLine()) != null) {
             line = rd.readLine();
            linea = line.split(",");//separador 
            activity.add(linea[2]);//registro de actividades
           
            }
        System.out.println(activity);//imprimimos

In this case I only want to save the activities of my registry, so I keep them in an array using split that searches inside my file, I give it the position in this case it takes it from position 0 and my activities are in position 2 and then print them to confirm that they are only activities. And the separator depends how you have it in your file or defined in your project can be by: (",") or (";") or (".").

    
answered by 27.02.2018 в 17:12