CSV file reading

Source: Internet
Author: User
Tags csv parser

First download the jar package from Com. ostermiller. util CVs at this http://ostermiller.org/utils/download.html address. There are two jar packages suitable for JDK 1.4 and JDK 1.5 respectively.
 

I used a jar package suitable for JDK 1.4 ostermiller utils version 1.05.00 for Java 1.4.

Public   Class Csvfileparser {

Private Labeledcsvparser csvparser; // CSV parser. the header information of the first line is automatically loaded as an index keyword.

Private   Int Currlinenum =   - 1 ; // Number of lines read by the file

Private String [] currline =   Null ; // Used to store data of the current row


/**/ /*
* Constructor,
* Param: information stream to be parsed in inputstream
* Throws ioexception
*/

Protected Csvfileparser (inputstream in) Throws Ioexception {

Csvparser =   New Labeledcsvparser ( New Excelcsvparser (in ));
Currlinenum = Csvparser. getlastlinenumber ();


/**/ /*
* Check whether data exists.
*
* Return true has a row of data, and false has no data.
*/
Public   Boolean Hasmore () Throws Ioexception {
Currline = Csvparser. Getline ();
Currlinenum = Csvparser. getlastlinenumber ();
If ( Null   = Currline)
Return   False ;
Return   True ;
}  

/**/ /*
* Returns the data of the current row. The data pointed to by the keyword
* Param: String filedname indicates the header of the row.
* Return: String returns the data of the current row. The data pointed to by the keyword
*/
Public String getbyfieldname (string fieldname) {

ReturnCsvparser. getvaluebylabel (fieldname );
}  

/**/ /*
* Close the parser
*
*
*/
Public   Void Close () Throws Ioexception {
Csvparser. Close ();

}  

/**/ /*
* Read data from the current row
*
* Return string [] reads data from the current row
*/
Public String [] Readline () Throws Ioexception {
Currline=Csvparser. Getline ();

Currlinenum=Csvparser. getlastlinenumber ();

ReturnCurrline;


}  

Public Getcurrlinenum () {

ReturnCurrlinenum;

}

Public   Static   Void Main (string [] ARGs) Throws Exception {

// Create resolution Information Flow
Inputstream in = New Fileinputstream ( New File ( " C: perflogsdata_000002.csv " ));

// Instance parser csvfileparser
Csvfileparser parser = New Csvfileparser (in );

// Read data
While (Parser. hasmore ()) {

System. Out. Print (parser. getbyfieldname ( " Time " ) + "   " ); // Time indicates the header data
System. Out. Print (parser. getbyfieldname ( " Total " ) + "   " );
System. Out. Print (parser. getbyfieldname ( " DPC time " ) + "   " );
System. Out. Print (parser. getbyfieldname ( " Interrupt time " ) + "   " );
System. Out. Print (parser. getbyfieldname ( " Processor time " ) + "" );

}

Parser. Close ();


}  

}  

 

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.