首页 > 其他 > 详细

lal

时间:2014-02-15 22:39:43      阅读:843      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class CsvResolve {

    public List<String[]> readCsv(String csvfilePath,String encodingCharSet)
    {
        List<String[]> resultList=new ArrayList<String[]>();
        try {
            FileInputStream csvfileInputStream = new FileInputStream(csvfilePath); 
            InputStreamReader csvfileInputStreamReader = new InputStreamReader(csvfileInputStream , encodingCharSet); 
            BufferedReader csvfileBufferedReader = new BufferedReader(csvfileInputStreamReader); 
            String line = csvfileBufferedReader.readLine();
            String[] firstLineElements=line.split("\t");
            //输出第一行的内容
            for(int i=0;i<firstLineElements.length;i++)
            {
                System.out.println(i+"\t"+firstLineElements[i]);
            }
            System.out.println();

            
            while ((line = csvfileBufferedReader.readLine()) != null) {
                String[] otherLineElements=line.split("\t");
                //如果结尾处有一个或多个tab键,说明,最后缺失的有元素
                //先把字符串数组转化为list,让后添加元素,然后再把添加完元素的list
                //转化为字符串数组
                
                if(otherLineElements.length<firstLineElements.length)
                {
                    String[] toaddOtherLineElements;
                    List<String> tempList=new ArrayList<String>();
                    int i=0;
                    for(;i<otherLineElements.length;i++)
                    {
                        if(otherLineElements[i].equals(""))
                        {
                            tempList.add(null);
                        }else{
                            tempList.add(otherLineElements[i]);
                        }
                    }
                    for(;i<firstLineElements.length;i++)
                    {
                        tempList.add(null);
                    }
                    toaddOtherLineElements=tempList.toArray(new String[1]);;
                    resultList.add(toaddOtherLineElements);
                }
                //如果长度相同说明最后的元素存在
                else{
                    String[] toaddOtherLineElements;
                    List<String> tempList=new ArrayList<String>();
                    int i=0;
                    for(;i<otherLineElements.length;i++)
                    {
                        if(otherLineElements[i].equals(""))
                        {
                            tempList.add(null);
                        }else{
                            tempList.add(otherLineElements[i]);
                        }
                    }
                    toaddOtherLineElements=tempList.toArray(new String[1]);;
                    resultList.add(toaddOtherLineElements);
                }
            }
            
            
            csvfileBufferedReader.close();
            

        } catch (FileNotFoundException e) {
            // 捕获File对象生成时的异常
            e.printStackTrace();
        } catch (IOException e) {
            // 捕获BufferedReader对象关闭时的异常
            e.printStackTrace();
        }
        return resultList;
        
    }
    
    public static void main(String[] args) {
        CsvResolve csvResolve=new CsvResolve();
        
        List<String[]> csvContent=csvResolve.readCsv("d:/csv/1.csv","utf-8");
        for(int i=0;i<csvContent.size();i++)
        {
            String[] temp=csvContent.get(i);
            for(int j=0;j<temp.length;j++)
            {
                System.out.println(j+"\t"+temp[j]);
            }
            System.out.println();
        }
        
    }
        
}
bubuko.com,布布扣

lal

原文:http://www.cnblogs.com/yufenghou/p/3550702.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!