user1843403
user1843403

Reputation: 11

Java, Turn Each Row of a CSV into a string

Hello I'm a newbie in Java using BlueJ. I have a csv file that contains a load of data in a table arrangement. I'm trying to find a way to take this information and find out how many comma separated values there are in the first row then regardless of rows put each comma separated value into an array.

Does anyone have any advice on how to do this?

Thanks in advance,
Harry.

Upvotes: 1

Views: 1152

Answers (3)

Ye Wint
Ye Wint

Reputation: 879

Here is another library to handle csv file. javacsv Code example is here

Upvotes: 0

João Simões
João Simões

Reputation: 1361

You can use the Scanner file, to read each line of the file, something similar to:

//  create a File object by giving the filepath
File file = new File("C:\\data.csv");

try {
    // Create a new scanner class that will read the file
    Scanner scanner = new Scanner(file);

    //  while the file has lines to read
    while (scanner.hasNextLine()) {

        //  read the line to a string
        String line = scanner.nextLine();

        //  do what you need with that line
    }
//  catch the exception if no file can be found
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Upvotes: 0

onon15
onon15

Reputation: 3630

CSV parsing can be tricky because of the need to support quoted values. I suggest not writing your own CSV parser, but using an existing library such as http://opencsv.sourceforge.net/.

Upvotes: 2

Related Questions