本文介绍了如何使用opencsv与我的集合(列表)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析2维列表,这里是我的模式列表:

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] ...

p>

I tried :

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

但我有这个错误:没有方法的签名:java.util.ArrayList.readAll适用于参数类型:()values:[]

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

也尝试:

 CSVWriter writer = new CSVWriter(new FileWriter("MyFile.csv"));
 String[] entries = list;
 writer.writeNext(entries);
 writer.close();

我得到一个csv文件,但只有:export.SampleJ:12432 SampleJ:12432,...

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

如何解析id,dateCreatedSample等等?

How can I parse id, dateCreatedSample, etc ?

推荐答案

您需要添加一个方法到 SampleJ 将其转换为 String [] ,例如

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());
}

这篇关于如何使用opencsv与我的集合(列表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:24