当前位置 : 主页 > 编程语言 > java >

csv逗号分隔符文件解析

来源:互联网 收集:自由互联 发布时间:2021-06-28
CsvParser.java import java.io.BufferedReader;import java.io.FileReader;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class CsvParser {// Saved input CSV file pathnamepr
CsvParser.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CsvParser {
	// Saved input CSV file pathname
	private String inputCsvFile;
	
	// Space mark , ; : etc.
	private String spaceMark=",";
	
	private Object[] arr = null;
	/**
	 * Constructor
	 * @param inputCsvFile
	 * @param spaceMark
	 */
	public CsvParser(String inputCsvFile,String spaceMark){
		this.inputCsvFile=inputCsvFile;
		this.spaceMark=spaceMark;
	}
	
	/**
	 * Constructor
	 * @param inputCsvFile
	 */
	public CsvParser(String inputCsvFile){
		this.inputCsvFile=inputCsvFile;
		this.spaceMark=",";
	}
	
	/**
	 * Get parsed array from CSV file
	 * @return
	 */
	public Object[] getParsedArray() {
		List
 
  > retval=new ArrayList
  
   >(); try { String regExp = getRegExp(); BufferedReader in = new BufferedReader(new FileReader(this.inputCsvFile)); String strLine; String str = ""; while ((strLine = in.readLine()) != null) { Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(strLine); List
   
     listTemp = new ArrayList
    
     (); 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 (isExisted("\"\"", str)) { str = str.replaceAll("\"\"", "\""); } } if (!"".equals(str)) { listTemp.add(str); } } // Add to retval retval.add(listTemp); } in.close(); } catch (Exception e) { e.printStackTrace(); } arr = retval.toArray(); return arr; } public void printArr() { if (arr != null) { for(Object obj : arr) { List
     
       list = (List
      
       ) obj; StringBuffer sb = new StringBuffer(); for(String item : list) { sb.append(item + ","); } sb.setLength(sb.length() - 1); System.out.println("[" + sb + "]"); } } } /** * 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 boolean isExisted(String argChar, String argStr) { boolean blnReturnValue = false; if ((argStr.indexOf(argChar) >= 0) && (argStr.indexOf(argChar) <= argStr.length())) { blnReturnValue = true; } return blnReturnValue; } }
      
     
    
   
  
 
网友评论