代码改变世界

java csv 文件 操作类

2013-06-23 22:48  youxin  阅读(878)  评论(1编辑  收藏  举报

一个CSV文件操作类,功能比较齐全:

package tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
//参考 http://wenku.baidu.com/view/dfc319c689eb172ded63b7ee.html
public class CsvUtil {
    private String fileName=null;
    private BufferedReader bufferedReader=null;
    private Vector v=new Vector();
    
    public CsvUtil(String filename) throws IOException
    {
        this.fileName=filename;
        bufferedReader=new BufferedReader(new FileReader(fileName));
        String stemp;
        while((stemp=bufferedReader.readLine())!=null)
        {
            if(!stemp.startsWith("#"))//以#开头表示注释
               v.add(stemp);
            
        }
        
    }
    public Vector getVector()
    {
        return v;
    }
    //得到CSV的行数
    public int getRowCount()
    {
        return v.size();
    }
    
    
    //取得指定行
    
    public String getRow(int index)
    {
        if(this.getRowCount()==0)
            return null;
        return (String)v.get(index);
    }
    //取得指定列
    public String getColumn(int index)
    {
        if(this.getColumnCount()==0)
        {
            return null;
        }
        StringBuffer scol=new StringBuffer();
        String temp=null;
        int column=this.getColumnCount();
        if(column>=1)
        {
            for(Iterator it=v.iterator();it.hasNext();)
            {
                temp=it.next().toString();
                scol=scol.append(temp.split(",")[index]+",");
                
            }
        }
     
        
        String str=new String(scol.toString());
        str=str.substring(0,str.length()-1);
        return str;
        
        
    }
    //得到列数
    public int getColumnCount()
    {
        if(!v.toString().equals("[]"))
        {
            if(v.get(0).toString().contains(","))
            {
                return v.get(0).toString().split(",").length;
            }
            else if(v.get(0).toString().trim().length()!=0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
            
        }
        else
        {
            return 0;
            
        }
        
    }
    
    
    //取得指定行,指定列的值
    public String getValueAt(int row,int col)
    {
        String temp=null;
        int column=this.getColumnCount();
        if(column>=1)
        {
            temp=v.get(row).toString().split(",")[col];
        }
         
        else
        {
            temp=null;
        }
        return temp;
        
    }
     
    public void insertRow(Vector v) throws IOException
    {
        //当前的vector增加
        // this.v.add错误
        
        BufferedWriter bw=new BufferedWriter(new FileWriter(this.fileName,true));//一定要接true,表示追加
        StringBuffer temp=new StringBuffer();
        Iterator  it=v.iterator();
        temp.append(it.next().toString());
        if(v.size()>1)
        {
            while(it.hasNext())
            {
                temp.append(","+it.next().toString());
            }
        }
        bw.write(temp.toString());
        bw.newLine();
        bw.flush();
        bw.close();
        
    }
    public void deleteRow(int index) throws IOException
    {
        v.remove(index);
        
        BufferedWriter bw=new BufferedWriter(new FileWriter(this.fileName));
        for(Iterator it=v.iterator();it.hasNext();)
        {
            bw.write(it.next().toString());
            bw.newLine();//一定要有
            
        }
        bw.flush();
        bw.close();
     
        
    }
    
 
    
    
    public void printAll()
    {
        Iterator it=v.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next().toString());
        }
    }
    
    
    public void CsvClose() throws IOException
    {
        this.bufferedReader.close();
    }

    
    public static void main(String[] args) throws IOException
    {
        CsvUtil cu=new CsvUtil("data/user.txt");
        /*
        String s11=cu.getValueAt(1, 1);
        System.out.println(s11);
        String arr1=cu.getRowCount(0);
        System.out.println(arr1);
        System.out.println(cu.getColumn(0));
        */
        Vector v=new Vector();
        v.add("user5");
        v.add("pwd5");
         cu.insertRow(v);
        //cu.deleteRow(2);
        cu.printAll();
     
    }
}