csv文件的读出

首先到该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包。

ExpandedBlockStart.gif ContractedBlock.gif public   class  CsvFileParser dot.gif
InBlock.gif
InBlock.gif    
private LabeledCSVParser csvParser;//csv解析器,对于第一行的表头信息,自动加载为索引关键字 
InBlock.gif

InBlock.gif    
private int currLineNum = -1;//文件所读到行数 
InBlock.gif

InBlock.gif    
private String[] currLine = null;//用来存放当前行的数据
InBlock.gif
   
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     *  构造函数,
InBlock.gif     *  Param: in InputStream 要解析的信息流
InBlock.gif     *  throws IOException
ExpandedSubBlockEnd.gif     
*/
  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected CsvFileParser(InputStream in) throws IOException dot.gif{
InBlock.gif        
InBlock.gif            csvParser 
= new LabeledCSVParser(new ExcelCSVParser(in));
InBlock.gif            currLineNum 
= csvParser.getLastLineNumber();
InBlock.gif              
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * 检查是否还有数据
InBlock.gif     *
InBlock.gif     * return ture 还有一行数据,false 没有数据
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public boolean hasMore() throws IOException dot.gif{
InBlock.gif        currLine 
= csvParser.getLine();
InBlock.gif        currLineNum 
= csvParser.getLastLineNumber();
InBlock.gif        
if (null == currLine)
InBlock.gif            
return false;
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * 返回当前行数据,关键字所指向的数据
InBlock.gif     * param:String filedName 该行的表头
InBlock.gif     * return:String 返回当前行数据,关键字所指向的数据
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getByFieldName(String fieldName) dot.gif{
InBlock.gif               
InBlock.gif        
return csvParser.getValueByLabel(fieldName);
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * 关闭解析器
InBlock.gif     *
InBlock.gif     * 
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void close() throws IOException dot.gif{
InBlock.gif        csvParser.close(); 
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif     * 读取当前行数据
InBlock.gif     *
InBlock.gif     *  return String[] 读取当前行数据
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String[] readLine() throws IOException dot.gif{
InBlock.gif        currLine 
= csvParser.getLine(); 
InBlock.gif
InBlock.gif        currLineNum 
= csvParser.getLastLineNumber(); 
InBlock.gif
InBlock.gif        
return currLine;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
public getCurrLineNum()dot.gif
InBlock.gif
InBlock.gif         
return currLineNum; 
InBlock.gif
ExpandedSubBlockEnd.gif  }
  
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exception dot.gif
InBlock.gif
InBlock.gif         
//创建解析信息流
InBlock.gif
        InputStream in=new FileInputStream(new File("C:PerfLogsdata_000002.csv")); 
InBlock.gif
InBlock.gif       
//实例解析器CsvFileParser 
InBlock.gif
        CsvFileParser parser=new CsvFileParser(in); 
InBlock.gif
InBlock.gif       
//读取数据
ExpandedSubBlockStart.gifContractedSubBlock.gif
        while(parser.hasMore())dot.gif{
InBlock.gif           
InBlock.gif            System.out.print(parser.getByFieldName(
"time")+" ");//time 系表头数据
InBlock.gif
            System.out.print(parser.getByFieldName("total")+" ");
InBlock.gif            System.out.print(parser.getByFieldName(
"dpc time")+" ");
InBlock.gif            System.out.print(parser.getByFieldName(
"Interrupt Time")+" ");
InBlock.gif            System.out.print(parser.getByFieldName(
"Processor Time")+"");
InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif        parser.close();
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 



 

转载于:https://www.cnblogs.com/sharewind/archive/2007/04/29/732714.html

你可能感兴趣的:(csv文件的读出)