再上一段希望生成上述csv的代码:
package chapter4;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by MyWorld on 2016/3/23.
*/
public class CsvWriteWithDoubleQuotation {
public static void main(String[] args) throws IOException {
List source = getResult();
CsvWriteWithDoubleQuotation csvWriter = new CsvWriteWithDoubleQuotation();
csvWriter.write(source);
}
private void write(List source) throws IOException {
File file = new File("resultWithDoubleQuotation.csv");
System.out.println(file.getAbsolutePath());
FileWriter fw = new FileWriter(file);
String title = "id,Name,Desc";
fw.write(title + "\n");
for (Peron peron : source) {
fw.write(String.format("%s,%s,\"%s\"\n", peron.getId(), peron.getName(), peron.getDesc()));
}
fw.flush();
fw.close();
}
private static List getResult() {
List source = new ArrayList();
source.add(new Peron(1, "Tom", "I li kui, nicknamed \"black tornado\""));
source.add(new Peron(2, "Jim", "I'm Jim"));
source.add(new Peron(3, "John", "I'm John,twenty years old."));
return source;
}
}
class Peron {
private final int id;
private final String name;
private final String desc;
public Peron(int id, String name, String desc) {
this.id = id;
this.name = name;
this.desc = desc;
} public int getId() {
return id;
} public String getName() {
return name;
}
public String getDesc() {
return desc;
}
}