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 ();
}
}