package com.lhx.test.utils; import com.alibaba.dubbo.common.utils.CollectionUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; /** * Created by lhx on 2019/6/18 * Desc : */ public class CSVRead { private BufferedReader br; //读取文件时,是否还有下一行 private boolean hasNext = true; //元素之间的分隔符 private char elementSeparator; //引号 private char quoteChar; //转义符 private char escape; //跳过的行数 private int skipLines; //是否跳过行 private boolean linesSkipped = true; private static final char DEFAULT_SEPARATOR = ','; private static final int INITIAL_READ_SIZE = 64; private static final char DEFAULT_QUOTE_CHARACTER = '"'; private static final char DEFAULT_ESCAPE_CHARACTER = '\\'; private static final int DEFAULT_SKIP_LINES = 0; // 解析文件核心代码 private ArrayList<String> parseNextLine(String nextLine) { ArrayList<String> inner = new ArrayList<String>(); StringBuilder sb = new StringBuilder(INITIAL_READ_SIZE); //是否在引用符号约束之内,为true时表示目前已经遍历到引号之内的字符了,默认不在引号之内,所以使用do while循环,强制第一次进行循环 boolean within = false; /*do {*/ for (int index = 0; index < nextLine.length(); index++) { char chr = nextLine.charAt(index); //如果当前符号为转义符,则取转义符后面的字符,同时索引前进一位,获取转义符后面的一位,拼接到结果字符串中 if (escape == chr) { //如果下一个字符在引用符号范围内,且该字符后面以为不是转义符也不是引用符,则认为
读取CSV文件,并解析为list
最新推荐文章于 2024-06-28 17:54:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)