标签:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.boguan.bte.service.common.IExcelRowReader; /** * 名称: CsvParser.java<br> * 描述: <br> * 类型: JAVA<br> * 最近修改时间:2016年7月6日 上午10:22:34<br> * * @since 2016年7月6日 * @author */ public class CsvReader { /** * Space mark , ; : etc. */ private String spaceMark = ","; /** * CSV固定只有SHEET_NUM=0 */ private final static int SHEET_NUM = 0; /** * Contructor * * @param inputCsvFile */ public CsvReader() { } /** * 行读取 */ private IExcelRowReader rowReader; public void setRowReader(IExcelRowReader rowReader) { this.rowReader = rowReader; } /** * 从CSV文件中获取数组 * * @return * @throws IOException */ public void process(String inputCsvFile) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new FileReader(inputCsvFile)); List<List<String>> retval = new ArrayList<List<String>>(); String regExp = getRegExp(); String strLine; String str = ""; int rowNum = 0; List<String> listTemp = null; while ((strLine = in.readLine()) != null) { Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(strLine); listTemp = new ArrayList<String>(); while (matcher.find()) { str = matcher.group(); str = str.trim(); if (str.endsWith(spaceMark)) { str = str.substring(0, str.length() - 1); str = str.trim(); } if (str.startsWith("\"") && str.endsWith("\"")) { str = str.substring(1, str.length() - 1); if (CsvReader.isExisted("\"\"", str)) { str = str.replaceAll("\"\"", "\""); } } if (!"".equals(str)) { listTemp.add(str); }else{ listTemp.add(" "); } } retval.add(listTemp); rowReader.getRows(SHEET_NUM, rowNum, listTemp); rowNum++; } } finally { if (in != null) { in.close(); } } } /** * Regular Expression for CSV parse * * @return */ private String getRegExp() { final String SPECIAL_CHAR_A = "[^\",\\n ]"; final String SPECIAL_CHAR_B = "[^\"" + spaceMark + "\\n]"; StringBuffer strRegExps = new StringBuffer(); strRegExps.append("\"(("); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*[" + spaceMark + "\\n ])*("); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*\"{2})*)*"); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*\"[ ]*" + spaceMark + "[ ]*"); strRegExps.append("|"); strRegExps.append(SPECIAL_CHAR_B); strRegExps.append("*[ ]*" + spaceMark + "[ ]*"); strRegExps.append("|\"(("); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*[" + spaceMark + "\\n ])*("); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*\"{2})*)*"); strRegExps.append(SPECIAL_CHAR_A); strRegExps.append("*\"[ ]*"); strRegExps.append("|"); strRegExps.append(SPECIAL_CHAR_B); strRegExps.append("*[ ]*"); return strRegExps.toString(); } /** * If argChar is exist in argStr * * @param argChar * @param argStr * @return */ private static boolean isExisted(String argChar, String argStr) { boolean blnReturnValue = false; if ((argStr.indexOf(argChar) >= 0) && (argStr.indexOf(argChar) <= argStr.length())) { blnReturnValue = true; } return blnReturnValue; } }
标签:
原文地址:http://www.cnblogs.com/wshsdlau/p/5646497.html