應用Javacsv.jar的jar包操作csv文件的辦法。本站提示廣大學習愛好者:(應用Javacsv.jar的jar包操作csv文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是應用Javacsv.jar的jar包操作csv文件的辦法正文
CSV其實就是COMMA SEPARATED VALUE的縮寫。csv文件是分隔文件,假如應用java的io流來寫,比擬費事,這裡為年夜家供給一個javacsv的jar包,這個很便利操作csv文件。
下載地址:http://xiazai.jb51.net/201608/yuanma/javcsv(jb51.net).rar
那若何應用呢?
只需看看以下一個例子,您一下就明確了。
import com.csvreader.CsvReader; import com.csvreader.CsvWriter; import org.junit.Test; import java.io.IOException; import java.nio.charset.Charset; /** * Created by javalittleman on 2016/8/18. */ public class TestCVS { /** * CSV導出 * * @throws Exception */ @Test public void exportCsv() throws IOException { String srcCSV = "F:/cnt_programa.csv"; String targetFile = "F:/test.csv"; CsvReader reader = new CsvReader(srcCSV, ',', Charset.forName("UTF-8")); CsvWriter write =new CsvWriter(targetFile,',',Charset.forName("UTF-8")); //各字段以引號標志 write.setForceQualifier(true); //途經表頭 //r.readHeaders(); //逐條讀取記載,直至讀完 String[] header = {}; while (reader.readRecord()) { //把頭保留起來 if (reader.getCurrentRecord()==0){ header = reader.getValues(); } //獲得以後記載地位 System.out.print(reader.getCurrentRecord() + "."); //讀取一筆記錄 System.out.println(reader.getRawRecord()); String[] tmp = {reader.getValues()[0],reader.getValues()[1]}; //修正記載,並只寫入第一個字段和第二字段 if (!header[1].equals(tmp[1]) && ("".equals(tmp[1])||tmp==null)){ tmp[1]="空"; write.writeRecord(tmp); }else{ write.writeRecord(new String[]{reader.getValues()[0],reader.getValues()[1]}); } } reader.close(); write.close(); } }
cnt_programa.csv文件:
"id","pid","no","serial","name","createtime" "100000","","No100000","","公司消息","2016/8/23 17:12:09" "100001","","No100001","","熱門資訊","2016/8/24 17:12:36" "100046","100001","No100046","1","銀行為態","2016/8/1 10:36:31" "100052","100001","No100052","2","司法律例","2016/8/2 20:39:10" "100088","100001","No100088","3","專業文庫","2016/8/5 19:05:47"
test.csv
"id","pid" "100000","空" "100001","空" "100046","100001" "100052","100001" "100088","100001"
以上所述是小編給年夜家引見的應用Javacsv.jar的jar包操作csv文件的辦法,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!