开发者

How to use opencsv with my collection (list)?

I need to parse 2 dimensional list, here is my schema list :

list (size=12) 
->[0] = "export.SampleJ:12432"  
   --> id = 12432  
   --> dateCreatedSample = "Tue Feb 03 19:04:23 CST 2009"  
   --> ttId = 0  
   --> chipId = 1012  
   --> ...  
->[1] = "export.SampleJ:12433"
   --> id = 12433
   --> ...
->[2] ...

I tried :

 List<String[]> allElements = list.readAll();
 StringWriter sw = new StringWriter();
 CSVWriter writer = new CSVWriter(sw);
 writer.writeAll(allElements);

but I have this error : No signature of method: java.util.ArrayList.readAll() is applicable for argument types: () values: []

Also tried :

 CSVWriter writer = new CSVWriter(new FileWriter("MyFile.csv"));
 String[] entries = li开发者_如何学Cst;
 writer.writeNext(entries);
 writer.close();

I get a csv file, but with only : "export.SampleJ:12432", "export.SampleJ:12432", ...

How can I parse id, dateCreatedSample, etc ?

Is there a way to connect my list directly with opencsv ?


You need to add a method to SampleJ to convert it into a String[], e.g.

class SampleJ {
    // ...
    public String[] toStringArray =
        Arrays.asList(String.valueOf(getId()),
                      String.valueOf(getDateCreatedSample()),
                      String.valueOf(getTtId()),
                      String.valueOf(getChipId()));
}

then loop to write all lines, e.g.

for (SampleJ elem: list) {
    writer.writeNext(elem.toStringArray());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜