• CSV文件解析工具


    package com.common.util;
    import java.io.BufferedReader;  
    import java.io.FileInputStream;  
    import java.io.IOException;  
    import java.io.InputStreamReader;  
    import java.util.ArrayList;  
    import java.util.List;  
    import java.util.regex.Matcher;  
    import java.util.regex.Pattern; 
    /**
     * excel与csv解析
     * 
     * @author Administrator
     *
     */
    public class ExcelAndCsvRead {
         private InputStreamReader fr = null;  
            private BufferedReader br = null;  
          
           public ExcelAndCsvRead(String f) throws IOException {  
               fr = new InputStreamReader(new FileInputStream(f));  
           }  
           /** 
             * 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中 
             */  
            public List<List<String>> readCSVFile() throws IOException {  
                br = new BufferedReader(fr);  
                String rec = null;// 一行  
                String str;// 一个单元格  
                List<List<String>> listFile = new ArrayList<List<String>>();  
                try {  
                    // 读取一行  
                    while ((rec = br.readLine()) != null) {  
                        Pattern pCells = Pattern  
                                .compile("("[^"]*("{2})*[^"]*")*[^,]*,*");  
                        Matcher mCells = pCells.matcher(rec);  
                        List<String> cells = new ArrayList<String>();// 每行记录一个list  
                        // 读取每个单元格  
                        while (mCells.find()) {  
                            str = mCells.group();  
                            str = str.replaceAll(  
                                    "(?sm)"?([^"]*("{2})*[^"]*)"?.*,", "$1");  
                            str = str.replaceAll("(?sm)("("))", "$2");  
                            cells.add(str);  
                        }  
                        listFile.add(cells);  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();  
                } finally {  
                    if (fr != null) {  
                        fr.close();  
                    }  
                    if (br != null) {  
                        br.close();  
                    }  
                }  
                return listFile;  
            }  
          
        //测试
    public static void main(String[] args) throws Throwable { ExcelAndCsvRead test = new ExcelAndCsvRead("C://Users//Administrator//AppData//Local//Temp//moban.csv"); //传入需要解析的文件路径 List<List<String>> csvList = test.readCSVFile(); System.out.println(csvList.get(0)); //解析文件的title System.out.println(csvList.get(1)); //文件的第一行数据 System.out.println(csvList.get(2));//文件的第二行数据 } }
  • 相关阅读:
    8086汇编学习小记王爽汇编语言实验12
    8086汇编学习小记王爽汇编语言课程设计1
    activeMQ 持久化配置 kevin
    snmpwalk kevin
    tcp benchmark kevin
    apache camel 条件路由 kevin
    netty 并发访问测试配置 kevin
    snmp常见操作 kevin
    转发:RocketMQ与kafka的对比 kevin
    centos jdk 下载 kevin
  • 原文地址:https://www.cnblogs.com/guokai870510826/p/5792713.html
Copyright © 2020-2023  润新知