csv檔案的讀出

來源:互聯網
上載者:User

首先到該http://ostermiller.org/utils/download.html 地址下載com.Ostermiller.util cvs的jar包。有分別適合jdk 1.4 和jdk 1.5的兩個jar包。
 

我用的系適合jdk 1.4的Ostermiller Utils version 1.05.00 for Java 1.4 的jar包。

public class CsvFileParser{ 

    private LabeledCSVParser csvParser;//csv解析器,對於第一行的表頭資訊,自動載入為索引關鍵字 

    private int currLineNum = -1;//檔案所讀到行數 

    private String[] currLine = null;//用來存放當前行的資料
   

    /**//*
     *  建構函式,
     *  Param: in InputStream 要解析的資訊流
     *  throws IOException
     */  

    protected CsvFileParser(InputStream in) throws IOException {
        
            csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
            currLineNum = csvParser.getLastLineNumber();
              

    /**//*
     * 檢查是否還有資料
     *
     * return ture 還有一行資料,false 沒有資料
     */
    public boolean hasMore() throws IOException {
        currLine = csvParser.getLine();
        currLineNum = csvParser.getLastLineNumber();
        if (null == currLine)
            return false;
        return true;
    } 

    /**//*
     * 返回當前行資料,關鍵字所指向的資料
     * param:String filedName 該行的表頭
     * return:String 返回當前行資料,關鍵字所指向的資料
     */
    public String getByFieldName(String fieldName) {
               
        return csvParser.getValueByLabel(fieldName);
    } 

    /**//*
     * 關閉解析器
     *
     * 
     */
    public void close() throws IOException {
        csvParser.close(); 

    } 

    /**//*
     * 讀取當前行資料
     *
     *  return String[] 讀取當前行資料
     */
    public String[] readLine() throws IOException {
        currLine = csvParser.getLine(); 

        currLineNum = csvParser.getLastLineNumber(); 

        return currLine;


    } 

   public getCurrLineNum(){ 

         return currLineNum; 

  }  

    public static void main(String[] args) throws Exception { 

         //建立解析資訊流
        InputStream in=new FileInputStream(new File("C:PerfLogsdata_000002.csv")); 

       //執行個體解析器CsvFileParser 
        CsvFileParser parser=new CsvFileParser(in); 

       //讀取資料
        while(parser.hasMore()){
           
            System.out.print(parser.getByFieldName("time")+" ");//time 系表頭資料
            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();
        

    } 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.