Java读取CSV文件(CSV文件数据内容包含逗号处理)

最近在公司写项目时,有个导入csv格式文件数据的需求。Java读取csv文件时默认是按照 ,[英文逗号]分割的,若是数据内容不包含逗号的话就简单多了,但遇到的问题就恰巧是尴尬的地方。

如果你看到这篇文章,应该也是遇到相同的问题了吧

1.1 解决方案一(推荐)

pom.xml

<dependency> 

     <groupId>com.opencsv</groupId> 

     <artifactId>opencsv</artifactId>

     <version>4.4</version>

</dependency>


1.2 代码示例

public void readCSV() {

        String srcPath = "D:\\data\\line.csv";

        String charset = "utf-8";

        try (CSVReader csvReader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(new FileInputStream(new File(srcPath)), charset))).build()) {

            Iterator<String[]> iterator = csvReader.iterator();

            while (iterator.hasNext()) {

                Arrays.stream(iterator.next()).forEach(System.out::print);

                System.out.println();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


2.1 解决方案二

看到的文章中,觉得比较好的解决方案就是使用正则进行匹配,读取的csv数据默认是用双引号包起来的,在最后的截取中,如果只按照双引号外的逗号截取,不就是能得到想要的数据了。

2.1 代码片段

/**

  * @param srcPath  csv文件路径

  */

private void readCSVFileData(String srcPath) {


        BufferedReader reader = null;

        String line = null;

        try {

            reader = new BufferedReader(new FileReader(srcPath));

        } catch (FileNotFoundException e) {

            logger.error("[读取CSV文件,插入数据时,读取文件异常]");

            e.printStackTrace();

        }

        String[] fieldsArr = null;

        int lineNum = 0;

        int insertResult = 0;

        TableInfo tableInfo = new TableInfo();

        tableInfo.setTableName(tableName);

        try {

            List listField;

            while ((line = reader.readLine()) != null) {

                if (lineNum == 0) {

                    //表头信息

                    fieldsArr = line.split(",");

                } else {

                    //数据信息

                    listField = new ArrayList<>();

                    String str;


                    line += ",";             

                    Pattern pCells = Pattern

                            .compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");

                    Matcher mCells = pCells.matcher(line);

                    List cells = new LinkedList();//每行记录一个list

                    //读取每个单元格

                    while (mCells.find()) {

                        str = mCells.group();

                        str = str.replaceAll(

                                "(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");

                        str = str.replaceAll("(?sm)(\"(\"))", "$2");

                        cells.add(str);

                    }

                    //从第2行起的数据信息list

                    listField.add(cells);

                }

                lineNum++;

            }


        } catch (Exception e) {

            e.printStackTrace();   

        }

    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 7,083评论 0 4
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,216评论 0 13
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 10,221评论 1 114
  • 一. Java基础部分.................................................
    wy_sure阅读 9,296评论 0 11
  • 50道经典Java编程练习题,将数学思维运用到编程中来。抱歉哈找不到文章的原贴了,有冒犯的麻烦知会声哈~ 1.指数...
    OSET我要编程阅读 11,974评论 0 9

友情链接更多精彩内容