Read CSV record from a String

62 views
Skip to first unread message

ehs cha

unread,
Aug 13, 2015, 10:52:08 AM8/13/15
to CSVChat
Hi all
I have a problem with Java CSV when I want to make a CsvRead object. I want the CSV records are read from a string. For that, I wrote the below code:


String[] str = {"name=\"bob\",family=\"Jackson\"", "name=\"Andy\",family=\"Richardson\""}

CsvReader csvReader;
for (String aStr : str){
//
csvReader = CsvReader.parse(aStr); <<--- Doesn't worked either
csvReader
= new CsvReader(
           
new ByteArrayInputStream(aStr.getBytes(Charset.forName("ISO-8859-1"))),
           
',',
           
Charset.defaultCharset());
   
   
for (int i=0; i<csvReader.getColumnCount(); i++){
       
System.out.print("%s = %s, ", csvReader.getHeader(i), csvReader.get(i));
   
}
   
System.out.println("\n\n");
}


I want to print the header an their value. Whats wrong?
Thanks in advanced.

shriop

unread,
Aug 13, 2015, 1:33:42 PM8/13/15
to CSVChat
You're leaving out a few steps. You need to call readHeaders() once after you construct the csvReader object, then create a "while" loop over calls to readRecord() , like the code example at http://www.csvreader.com/java_csv_samples.php .

Bruce Dunwiddie

ehs cha

unread,
Aug 15, 2015, 2:38:44 AM8/15/15
to CSVChat
Thanks Bruce
Reply all
Reply to author
Forward
0 new messages