Javacsv is a foreign development of a relatively good operation of the CSV file API, here a brief talk about usage.
First download the Javacsv2.0.zip file, after decompression, add Javacsv.jar to the project.
Site Download Address:
Http://www.cnitblog.com/Files/rd416/javacsv2.0.zip
Official Download Address:
http://sourceforge.net/project/showfiles.php?group_id=33066
Simple Operation code:
/** * Read CSV file/public void Readecsv () {try { arraylist<string[]> csvlist = new arraylist<string[]> ();
Used to save the data String Csvfilepath = "C:/test.csv"; Csvreader reader = new Csvreader (Csvfilepath, ', ', Charset.forname ("Sjis")); Generally read with this code can be reader.readheaders ();
Skip the header If you need a table head, don't write this sentence.
while (Reader.readrecord ()) {//Read the Data Csvlist.add (Reader.getvalues ()) In addition to the header line by row;
} reader.close (); for (int row=0;row<csvlist.size (); row++) {String cell = Csvlist.get (ro W) [0];
Obtain the data System.out.println (cell) in column No. 0 of row rows;
}
}catch (Exception ex) {System.out.println (ex);
}/** * Write CSV file/public void Writecsv () {
try {String Csvfilepath = "C:/test.csv";
Csvwriter WR =new Csvwriter (Csvfilepath, ', ', Charset.forname ("Sjis"));
String[] contents = {"AAAAA", "bbbbb", "CCCCCC", "ddddddddd"};
Wr.writerecord (contents);
Wr.close ();
catch (IOException e) {e.printstacktrace (); }
}
For more functions, see the javacsv2.0/doc/index.html description. I think it's useful to see Javacsv2.0/src/alltests.java.