Supercsv reading/writing CSV and TSV file instance tutorial

Source: Internet
Author: User
Tags valid email address

First, let's briefly introduce the differences between CSV and TSV files:

TSV, short for Tab-separated values, that is, Tab-separated values. For TSV standards, see: http://en.wikipedia.org/wiki/Tab-separated_values CSV, Comma-separated values, that is, Comma-separated values. For CSV standards, see: http://en.wikipedia.org/wiki/Comma-separated_values

The project needs to organize the original tsv file data to form a more convenient new tsv File (add several columns ). It involves reading and writing tsv files. In fact, self-implementation is also very simple, but there is a ready-made toolkit supercsv, it is used to try. Address: http://supercsv.sourceforge.net/index.html

The documentation is clear and clear. There are actually many examples of parsing csv files using supercsv on the internet. However, the difference between tsv and csv shows that a complete set of code can be solved, if you change the separator, you will be hungry. In supercsv, it does. First attach the example of the official website: http://supercsv.sourceforge.net/examples_reading.html

Resolved csv file:

UstomerNo, firstName, lastName, birthDate, mailingAddress, married, numberOfKids, expiration, email, loyaltyPoints 1, John, Dunbar, 13/06/1945, "1600 Amphitheatre Parkway Mountain View, CA 94043 United States "," May the Force be with you. "-Star Wars", jdunbar@gmail.com, 0 2, Bob, Down, 25/02/1919, "1601 Willow Rd. menlo Park, CA 94025 United States ", Y, 0," Frankly, my dear, I don't give a damn. ""-Gone Wit H The Wind ", bobdown@hotmail.com, 123456 3, Alice, Wunderland, 08/08/1985," One Microsoft Way Redmond, WA 98052-6399 United States ", Y, 0," "Play it, sam. play "" As Time Goes. "-Casablanca", throughthelookingglass@yahoo.com, 2255887799 4, Bill, Jobs, 10/07/1973, "2701 San Tomas Expressway Santa Clara, CA 95050 United States", Y, 3, "You 've got to ask yourself one question:" "Do I feel lucky? "" Well, do ya, punk? ""-Dirty Harry ", billy34@hotmail.com, 36

Code parsed using MapReader:

The code is as follows: Copy code
<Pre class = "brush: java">
/**
* An example of reading using CsvMapReader.
*/
Private static void readWithCsvMapReader () throws Exception {

ICsvMapReader mapReader = null;
Try {
MapReader = new CsvMapReader (new FileReader (CSV_FILENAME), CsvPreference. STANDARD_PREFERENCE );
           
// The header columns are used as the keys to the Map
Final String [] header = mapReader. getHeader (true );
Final CellProcessor [] processors = getProcessors ();
           
Map & lt; String, Object & gt; customerMap;
While (customerMap = mapReader. read (header, processors ))! = Null ){
System. out. println (String. format ("lineNo = % s, rowNo = % s, customerMap = % s", mapReader. getLineNumber (),
MapReader. getRowNumber (), customerMap ));
            }
           
    }
Finally {
If (mapReader! = Null ){
MapReader. close ();
            }
}}

/**
* Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
* Columns are read as null (hence the NotNull () for mandatory columns). ** @ return the cell processors
*/
Private static CellProcessor [] getProcessors (){

Final String emailRegex = "[a-z0-9. _] + @ [a-z0-9.] +"; // just an example, not very robust!
StrRegEx. registerMessage (emailRegex, "must be a valid email address ");
   
Final CellProcessor [] processors = new CellProcessor [] {
New UniqueHashCode (), // customerNo (must be unique)
New NotNull (), // firstName
New NotNull (), // lastName
New ParseDate ("dd/MM/yyyy"), // birthDate
New NotNull (), // mailingAddress
New Optional (new ParseBool (), // married
New Optional (new ParseInt (), // numberOfKids
New NotNull (), // favouriteQuote
New StrRegEx (emailRegex), // email
New LMinMax (0L, LMinMax. MAX_LONG) // loyaltyPoints
};
   
Return processors ;}
</Pre>

I am afraid the code of the sample can no longer be clear. The delimiter is set through CsvPreference. STANDARD_PREFERENCE. To parse the TSV file, replace it with CsvPreference TAB_PREFERENCE.

Add source code:

The code is as follows: Copy code
<Pre class = "brush: java">
/**
* Ready to use configuration that shoshould cover 99% of all usages.
*/
Public static final CsvPreference STANDARD_PREFERENCE = new CsvPreference. Builder ('"'," rn "). build ();

/**
* Ready to use configuration for Windows Excel exported CSV files.
*/
Public static final CsvPreference EXCEL_PREFERENCE = new CsvPreference. Builder ('"'," n "). build ();
 
/**
* Ready to use configuration for north European excel CSV files (columns are separated by ";" instead ",")
*/
Public static final CsvPreference EXCEL_NORTH_EUROPE_PREFERENCE = new CsvPreference. Builder ('"', ';'," n "). build ();
 
/**
* Ready to use configuration for tab-delimited files.
*/
Public static final CsvPreference TAB_PREFERENCE = new CsvPreference. Builder ('"', 'T'," n "). build ();

</Pre>

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.