Java 读取csv文件,经测可用

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
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=",";
    
    /**
     * Contructor
     * @param inputCsvFile
     */
    public CsvParser(String inputCsvFile,String spaceMark){
        this.inputCsvFile=inputCsvFile;
        this.spaceMark=spaceMark;
    }
    
    /**
     * Contructor
     * @param inputCsvFile
     */
    public CsvParser(String inputCsvFile){
        this.inputCsvFile=inputCsvFile;
        this.spaceMark=",";
    }
    
    /**
     * Get parsed array from CSV file
     * @return
     */
    public List<List<String>> getParsedArray() throws Exception{
        List<List<String>> retval=new ArrayList<List<String>>();
        
        String regExp = getRegExp();
        BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(this.inputCsvFile),"GBK")); 
//        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<String> 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 (CsvParser.isExisted("\"\"", str))
                    {
                        str = str.replaceAll("\"\"", "\"");
                    }
                }
                
                if (!"".equals(str))
                {
                    listTemp.add(str);
                }
            }
            
            retval.add(listTemp);     
        }
        in.close();
        
        return retval;
    }
    
    /**
     * 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;
    }
    
    public static void main(String[] args)  throws Exception{
        CsvParser parser=new CsvParser("C:\\Users\\XXYF21\\Downloads\\Run History.csv");
        
        List<List<String>> arr=parser.getParsedArray();
        
        for(List<String> obj:arr){
            System.out.print("[");
            
            for(String item:obj){
                System.out.print(item+",");
            }
            
            System.out.println("],");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值