應用:【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(); } }