How to manipulate a CSV file using a Javacsv.jar jar package _java

Source: Internet
Author: User

CSV is actually the abbreviation for comma separated value. CSV file is a separate file, if you use Java IO Stream to write, more trouble, here to provide you with a javacsv jar package, this is easy to manipulate the CSV file.

Download Address: Http://xiazai.jb51.net/201608/yuanma/javcsv (jb51.net). rar

So how do you use it?

Just look at one of the following examples and you'll see.

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 export * * @throws Exception/@Test public void Exportcsv () throws IOException {String s
Rccsv = "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"));
Each field is marked with quotation marks Write.setforcequalifier (true);
Passing the table Head//r.readheaders ();
Read a record, until read string[] header = {}; while (Reader.readrecord ()) {//head saved if (Reader.getcurrentrecord () ==0) {header = Reader.getvalues ();}//Get current record location Syste
M.out.print (Reader.getcurrentrecord () + ".");
Read a record System.out.println (Reader.getrawrecord ());
string[] tmp = {reader.getvalues () [0],reader.getvalues () [1]}; Modify the record and write only the first field and the second field if (!header[1].equals (tmp[1]) && ("". Equals (tmp[1)) | | tmp==null)) {tmp[1]= "empty"; Write.writerecord (TMP);}
else{Write.writerecord (New String[]{reader.getvalues () [0],reader.getvalues () [1]});}
Reader.close ();
Write.close ();  }
}

Cnt_programa.csv file:

"id", "pid", "No", "Serial", "Name", "Createtime" "100000", "" "," No100000 "," "
," Company News "," 2016/8/23 17:12:09 ""
100001 "," "," No100001 "," "," "Hot News", "2016/8/24 17:12:36"
"100046", "100001", "No100046", "1", "Bank dynamics", "2016/8/1 10:36:31 "
" 100052 "," 100001 "," No100052 "," 2 "," Laws and regulations "," 2016/8/2 20:39:10 "

Test.csv

"id", "pid"
"100000", "Empty"
"100001", "Empty"
"100046", "100001"
"100052", "100001"

The above is a small set to introduce the use of Javacsv.jar jar package operation csv file method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.