Read CSV in Java

Source: Internet
Author: User

First download the jar package from Com. ostermiller. util CVs at this http://ostermiller.org/utils/download.html address.

 

Public class csvfileparser {</P> <p> private labeledcsvparser csvparser; // CSV parser. For the header information of the first line, automatically loaded as index keywords </P> <p> private int currlinenum =-1; // number of lines read by the file </P> <p> private string [] currline = NULL; // used to store the data of the current row </P> <p>/* <br/> * constructor, <br/> * Param: in inputstream the information stream to be parsed <br/> * throws ioexception <br/> */</P> <p> protected csvfileparser (inputstream in) throws ioexception {</P> <p> csvparser = new labeledcsvparser (New excelcsvparser (in); <br/> currlinenum = csvparser. getlastlinenumber (); </P> <p>/* <br/> * check whether data exists. <br/> * return ture has a row of data, false no data <br/> */<br/> Public Boolean hasmore () throws ioexception {<br/> currline = csvparser. getline (); <br/> currlinenum = csvparser. getlastlinenumber (); <br/> If (null = currline) <br/> return false; <br/> return true; <br/>}</P> <p>/* <br/> * return data of the current row, data pointed to by the keyword <br/> * Param: string filedname header of the row <br/> * return: String returns the data of the current row, data pointed to by the keyword <br/> */<br/> Public String getbyfieldname (string fieldname) {</P> <p> return csvparser. getvaluebylabel (fieldname ); <br/>}</P> <p>/* <br/> * close the parser <br/> */<br /> Public void close () throws ioexception {<br/> csvparser. close (); </P> <p >}</P> <p>/* <br/> * read data from the current row <br/> * return string [] read data from the current row <br/> */<br/> Public String [] Readline () throws ioexception {<br/> currline = csvparser. getline (); </P> <p> currlinenum = csvparser. getlastlinenumber (); </P> <p> return currline; </P> <p >}</P> <p> Public getcurrlinenum () {</P> <p> return currlinenum; </P> <p >}</P> <p> Public static void main (string [] ARGs) throws exception {</P> <p> // create a resolution information stream <br/> inputstream in = new fileinputstream (new file ("C: perflogsdata_000002.csv ")); </P> <p> // instance parser csvfileparser <br/> csvfileparser parser = new csvfileparser (in ); </P> <p> // read data <br/> while (parser. hasmore () {</P> <p> system. out. print (parser. getbyfieldname ("time") + ""); // time indicates the header data <br/> system. out. print (parser. getbyfieldname ("Total") + ""); <br/> system. out. print (parser. getbyfieldname ("DPC time") + ""); <br/> system. out. print (parser. getbyfieldname ("interrupt time") + ""); <br/> system. out. print (parser. getbyfieldname ("processor time") + ""); </P> <p >}</P> <p> parser. close (); </P> <p >}< br/>

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.