应用:【CSV(逗号分隔值文件格式)_百度百科 (baidu.com)】
在程序之间转移表格数据,作为一种可选择的输入/输出格式
优点:
1.文件结构简单,基本上和文本的差别不大;
2.可以和microExcle进行转换,这是一个很大的优点,很容易进行察看模式转换,
但是如果你同样的csv文件和将其转换成xls文件后的size比较就更加明白他在size上的优势了。
3.由于其简单的存储方式,一方面可以减少存储信息的容量,这样有利于网络传输以及客户端的再处理;同时由于是一堆没有任何说明的数据,具备基本的安全性。
生成csv文件时的一个坑-百度经验 (baidu.com)——没有对字符串中的英文逗号进行特殊处理
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 CsvWriter { public static void main(String[] args) throws IOException { List<String> source = getResult(); CsvWriter csvWriter = new CsvWriter(); csvWriter.write(source); } private static List<String> getResult() { String title = "id,Name,Desc"; List<String> source = new ArrayList<String>(); source.add(title); source.add(String.format("1,Tom,%s", "My name is tom.")); source.add(String.format("2,Jim,%s", "My name is Jim,twenty years old")); source.add(String.format("3,John,%s", "My name is John.Hello!")); return source; } public void write(List<String> source) throws IOException { File file = new File("result.csv"); System.out.println(file.getAbsolutePath()); FileWriter fw = new FileWriter(file); for (String line : source) { fw.write(String.format("%s \n", line)); } fw.flush(); fw.close(); } }