Reading from java csv

Asked

Viewed 21 times

-1

Well I need to do a college job, where I have to read a csv and print your data on the screen. I am using the split method to store the line information in a String vector, and then Seto all the data and a class, but with I am filtering by "," the file the times of the stick because lines appear with this

0.0594,1921,0.982,['Sergei Rachmaninoff', 'James Levine', 'Berliner Philharmoniker'],0.279,831667,0.211,0,0,4BJqT0PrAfrxzMOxytFOIz,0.878,10,0.665,-20.09 6,1.0,"[Piano Concerto No. 3 in D Minor, Op. 30: III. Finale. Alla breve]",4,1921,0.0366,80.954

Summarizing that this is between "," and a position of the array, that is to say that this between brackets has q be just a position of the array ignoring the "," from inside the brackets and n I am able to make this filter.

This is my code:

public static List<Musica> lerMusica(List<Musica> listaMusica) throws NumberFormatException, ParseException, FileNotFoundException {
        String row;
        try {
            BufferedReader csvReader = new BufferedReader(new FileReader("C:/Users/Joseilton/Desktop/verde/Verde/tmp/dataAEDs.csv"));
            String[] musicaParaCriar = new String[18];
            int contLinhas = 0;
            int cont = 0;
            while ((row = csvReader.readLine()) != null) {
                String[] musica = row.split(",");
                if (contLinhas != 0) {
                    musicaParaCriar = musica;
                    Musica musicaCriada = criaMusica(musicaParaCriar);
                    listaMusica.add(musicaCriada);
                    cont++;
                }
                contLinhas += 1;
            }
            
            csvReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return listaMusica;
    }

If you can help, I’d appreciate it.

1 answer

0

I usually use the java.util.Scanner scanner class in these cases.;

// your code before the while

scanner = new Scanner(  row ).useDelimiter(",");

// then you access each field scanner.nextInt(); // if the value is integer

scanner.next();

// if it is String

and other types...

Browser other questions tagged

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