zoukankan      html  css  js  c++  java
  • lal

    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("	");
                //输出第一行的内容
                for(int i=0;i<firstLineElements.length;i++)
                {
                    System.out.println(i+"	"+firstLineElements[i]);
                }
                System.out.println();
    
                
                while ((line = csvfileBufferedReader.readLine()) != null) {
                    String[] otherLineElements=line.split("	");
                    //如果结尾处有一个或多个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+"	"+temp[j]);
                }
                System.out.println();
            }
            
        }
            
    }
  • 相关阅读:
    Android开发经验小节2:循环利用你的小对象
    新技术你知道吗?Node.js的HelloWorld!
    css添加省略号(twxtoverflow:ellipsis)/图标精灵(background)
    htmlcss优先级(style,id,class,tag,*,继承,!important)
    html标签样式(块,内联,内联块,按内容,按显示分类)
    html浮动(float)
    css盒子模型
    htmlcss继承(inherit)
    htmldisplay(转化元素,block,inline,inlineblock,none)
    html定位position(固定,相对(relative),绝对(absolute))
  • 原文地址:https://www.cnblogs.com/yufenghou/p/3550702.html
Copyright © 2011-2022 走看看