CSV file parsing

Source: Internet
Author: User

CSV (Comma separated value file format)
Comma-separated values (comma-separated values,csv, sometimes referred to as character-delimited values, because delimited characters can also be not commas), whose files store tabular data (numbers and text) in plain text. Plain text means that the file is a sequence of characters and does not contain data that must be interpreted like a binary number. A CSV file consists of any number of records separated by a newline character, each record consists of a field, and the delimiter between the fields is another character or string, most commonly a comma or tab. Typically, all records have exactly the same sequence of fields. The common criteria for the CSV file format do not exist, but there is a basic description in RFC 4180.      The character encoding used is also not specified, but 7-BITASCII is the most basic generic encoding. Java parsing csv file common method: Other Auxiliary Group class referencehttp://www.cnblogs.com/wshsdlau/p/5643862.html
import java.io.bufferedreader;import java.io.filereader;import java.io.ioexception;import java.util.ArrayList; Import Java.util.list;import Java.util.regex.matcher;import Java.util.regex.pattern;import Com.boguan.bte.service.common.IExcelRowReader;/** Name: csvparser.java<br> * Description: <br> * Type: java<br> * Last modified: July 6, 2016 morning 10:22:34<br> * * @s Ince July 6, 2016 * @author*/ Public classCsvreader {/** * Space mark,;: etc. */    PrivateString Spacemark =","; /** * CSV fixed only sheet_num=0*/    PrivateFinalStatic intSheet_num =0; /** * contructor * * @param inputcsvfile*/     PublicCsvreader () {}/** * Line read*/    PrivateIexcelrowreader Rowreader;  Public voidSetrowreader (Iexcelrowreader rowreader) { This. Rowreader =Rowreader; }    /** * get the array from the CSV file * * @return * @throws IOException*/     Public voidprocess (String inputcsvfile) throws IOException {BufferedReaderinch=NULL; Try {            inch=NewBufferedReader (NewFileReader (inputcsvfile)); List<List<String>> retval =NewArraylist<list<string>>(); String RegExp=Getregexp ();            String StrLine; String Str=""; intRowNum =0; List<String> listtemp =NULL;  while((StrLine =inch. ReadLine ())! =NULL) {pattern pattern=Pattern.compile (REGEXP); Matcher Matcher=Pattern.matcher (StrLine); Listtemp=NewArraylist<string>();  while(Matcher.find ()) {str=Matcher.group (); STR=Str.trim (); if(Str.endswith (Spacemark)) {str= Str.substring (0, Str.length ()-1); STR=Str.trim (); }                    if(Str.startswith ("\"") && Str.endswith ("\"") ) {str= Str.substring (1, Str.length ()-1); if(Csvreader.isexisted ("\"\"", str)) {STR= Str.replaceall ("\"\"","\""); }                    }                    if(!"". Equals (str))                    {Listtemp.add (str); }Else{Listtemp.add (" ");                }} retval.add (Listtemp);                Rowreader.getrows (Sheet_num, RowNum, listtemp); RowNum++; }        } finally {            if(inch!=NULL) {                inch. Close (); }        }    }    /** * Regular Expression for CSV parse * * @return*/    PrivateString Getregexp () {final String special_char_a="[^\ ", \\n]"; Final String Special_char_b="[^\""+ Spacemark +"\\n]"; StringBuffer Strregexps=NewStringBuffer (); Strregexps.append ("\"((");        Strregexps.append (special_char_a); Strregexps.append ("*["+ Spacemark +"\\n]) *(");        Strregexps.append (special_char_a); Strregexps.append ("*\ "{2}) *) *");        Strregexps.append (special_char_a); Strregexps.append ("*\ "[]*"+ Spacemark +"[]*"); Strregexps.append ("|");        Strregexps.append (Special_char_b); Strregexps.append ("*[]*"+ Spacemark +"[]*"); Strregexps.append ("|\"((");        Strregexps.append (special_char_a); Strregexps.append ("*["+ Spacemark +"\\n]) *(");        Strregexps.append (special_char_a); Strregexps.append ("*\ "{2}) *) *");        Strregexps.append (special_char_a); Strregexps.append ("*\ "[]*"); Strregexps.append ("|");        Strregexps.append (Special_char_b); Strregexps.append ("*[]*"); returnstrregexps.tostring (); }    /** * If Argchar is exist in ARGSTR * * @param argchar * @param argstr * @return*/    Private Staticboolean isexisted (String Argchar, String argstr) {Boolean blnreturnvalue=false; if((Argstr.indexof (Argchar) >=0) && (Argstr.indexof (Argchar) <=argstr.length ())) {Blnreturnvalue=true; }        returnBlnreturnvalue; }}

CSV file parsing

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.