首先到該http://ostermiller.org/utils/download.html 地址下載com.Ostermiller.util cvs的jar包。
public class CsvFileParser{ </p><p> private LabeledCSVParser csvParser;//csv解析器,對於第一行的表頭資訊,自動載入為索引關鍵字 </p><p> private int currLineNum = -1;//檔案所讀到行數 </p><p> private String[] currLine = null;//用來存放當前行的資料</p><p> /*<br /> * 建構函式,<br /> * Param: in InputStream 要解析的資訊流<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 /> * 檢查是否還有資料<br /> *<br /> * return ture 還有一行資料,false 沒有資料<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 /> * 返回當前行資料,關鍵字所指向的資料<br /> * param:String filedName 該行的表頭<br /> * return:String 返回當前行資料,關鍵字所指向的資料<br /> */<br /> public String getByFieldName(String fieldName) {</p><p> return csvParser.getValueByLabel(fieldName);<br /> } </p><p> /*<br /> * 關閉解析器<br /> *<br /> *<br /> */<br /> public void close() throws IOException {<br /> csvParser.close(); </p><p> } </p><p> /*<br /> * 讀取當前行資料<br /> *<br /> * return String[] 讀取當前行資料<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> //建立解析資訊流<br /> InputStream in=new FileInputStream(new File("C:PerfLogsdata_000002.csv")); </p><p> //執行個體解析器CsvFileParser<br /> CsvFileParser parser=new CsvFileParser(in); </p><p> //讀取資料<br /> while(parser.hasMore()){</p><p> System.out.print(parser.getByFieldName("time")+" ");//time 系表頭資料<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> } </p><p>}<br />