CSV is an acronym for comma-delimited files (Comma separated Values), a plain text format for storing data, typically used in spreadsheets or database software. In a CSV file, the data column is separated by commas, allowing the program to recreate the correct column structure for the data by reading the file, and start a new column each time a comma is encountered. Such as:
1, Zhang San, male 2, John Doe, male 3, Little red, female
1. CSV Import/Export wrapper class
Package Com.parami.utils;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.ioexception;import Java.io.OutputStreamWriter; Import Java.util.arraylist;import java.util.list;/** * CSV operation (export and import) * * @author Lin Yi-chin * @version 1.0 Jan 27, 2014 4:30:58 PM */public class Csvutils {/** * export * * @param file CSV file (path + file name), CSV file does not exist automatically created * @param dat Alist Data * @return */public static Boolean exportcsv (file file, List<string> dataList) {Boolean I Ssucess=false; FileOutputStream Out=null; OutputStreamWriter Osw=null; BufferedWriter Bw=null; try {out = new FileOutputStream (file); OSW = new OutputStreamWriter (out); Bw =new bufferedwriter (OSW); if (Datalist!=null &&!datalist.isempty ()) {for (String data:datalist) {Bw.appen d (data). APpend ("\ r"); }} issucess=true; } catch (Exception e) {issucess=false; }finally{if (bw!=null) {try {bw.close (); Bw=null; } catch (IOException e) {e.printstacktrace (); }} if (Osw!=null) {try {osw.close (); Osw=null; } catch (IOException e) {e.printstacktrace (); }} if (Out!=null) {try {out.close (); Out=null; } catch (IOException e) {e.printstacktrace (); }}} return issucess; }/** * Import * * @param file CSV (path + file) * @return */public static list<string> Impor Tcsv (file file) {List<sTring> datalist=new arraylist<string> (); BufferedReader Br=null; try {br = new BufferedReader (new FileReader (file)); String line = ""; while (line = Br.readline ())! = null) {Datalist.add (line); }}catch (Exception e) {}finally{if (br!=null) {try {Br.close (); Br=null; } catch (IOException e) {e.printstacktrace (); }}} return dataList; }}
2. CSV Import/Export test
Package Junit.test;import Java.io.file;import java.util.arraylist;import java.util.list;import org.junit.Test; Import com.parami.utils.csvutils;/** * CSV operation (export and import) * * @author Lin Yi-chin * @version 1.0 Jan, 4:17:02 PM */public class csvtest {/** * CSV export * * @throws Exception */@Test public void Exportcsv () {List<s Tring> datalist=new arraylist<string> (); Datalist.add ("1, Zhang San, male"); Datalist.add ("2, John Doe, Male"); Datalist.add ("3, Little Red, female"); Boolean issuccess=csvutils.exportcsv (New File ("D:/test/ljq.csv"), dataList); System.out.println (issuccess); }/** * CSV export * * @throws Exception */@Test public void ImportCSV () {List<strin G> datalist=csvutils.importcsv (New File ("D:/test/ljq.csv")); if (Datalist!=null &&!datalist.isempty ()) {for (String data:datalist) {System.out.print ln (data); } } } }
Java CSV XLS operations (export and import)