---
title: "java 解析csv文件"
author: "hwpok"
date: "2008-12-17T09:31:00Z"
tags: []
slug: "java-csv"
draft: false
lastmod: "2008-12-17T09:31:00Z"
id: 615782
url: "https://hwpok.iteye.com/blog/615782"
---

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.logging.Level;  
import java.util.logging.Logger;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;

/\**  
 * @author panhf2003  
 * @version 2008/09/05,  
 \*/

public class CsvFileUtil  
{  
      
    /\**  
     * 构造，禁止实例化  
     \*/  
    private CsvFileUtil()  
    {}  
      
    public static void main(String\[] args)  
    {  
        try  
        {  
            readCsvFile("d:\\\\ZD\_CUSTOMER\_VIEW.csv");  
        }  
        catch (FileNotFoundException ex)  
        {  
            Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,  
                    null, ex);  
        }  
        catch (IOException ex)  
        {  
            Logger.getLogger(CsvFileUtil.class.getName()).log(Level.SEVERE,  
                    null, ex);  
        }  
    }  
      
    /\**  
     * csv文件读取&lt;BR/&gt; 读取绝对路径为argPath的csv文件数据，并以List返回。  
     *   
     * @param argPath  
     *            csv文件绝对路径  
     * @return csv文件数据（List&lt;String\[]&gt;）  
     * @throws FileNotFoundException  
     * @throws IOException  
     \*/  
    public static List&lt;String\[]&gt; readCsvFile(String argPath)  
            throws FileNotFoundException, IOException  
    {  
        CsvFileUtil util = new CsvFileUtil();  
        File cvsFile = new File(argPath);  
        List&lt;String\[]&gt; list = new ArrayList&lt;String\[]&gt;();  
        FileReader fileReader = null;  
        BufferedReader bufferedReader = null;  
        try  
        {  
            fileReader = new FileReader(cvsFile);  
            bufferedReader = new BufferedReader(fileReader);  
            String regExp = util.getRegExp();  
              
            String strLine = "";  
            String str = "";  
            while ((strLine = bufferedReader.readLine()) != null)  
            {  
                Pattern pattern = Pattern.compile(regExp);  
                Matcher matcher = pattern.matcher(strLine);  
                List&lt;String&gt; listTemp = new ArrayList&lt;String&gt;();  
                while (matcher.find())  
                {  
                    str = matcher.group();  
                    str = str.trim();  
                    if (str.endsWith(","))  
                    {  
                        str = str.substring(0, str.length() - 1);  
                        str = str.trim();  
                    }  
                    if (str.startsWith("\\"") && str.endsWith("\\""))  
                    {  
                        str = str.substring(1, str.length() - 1);  
                        if (util.isExisted("\\"\\"", str))  
                        {  
                            str = str.replaceAll("\\"\\"", "\\"");  
                        }  
                    }  
                    if (!"".equals(str))  
                    {  
                        System.out.print(str + " ");  
                        listTemp.add(str);  
                    }  
                }  
                // test  
                System.out.println();  
                list.add((String\[]) listTemp  
                        .toArray(new String\[listTemp.size()]));  
            }  
        }  
        catch (FileNotFoundException e)  
        {  
            throw e;  
        }  
        catch (IOException e)  
        {  
            throw e;  
        }  
        finally  
        {  
            try  
            {  
                if (bufferedReader != null)  
                {  
                    bufferedReader.close();  
                }  
                if (fileReader != null)  
                {  
                    fileReader.close();  
                }  
            }  
            catch (IOException e)  
            {  
                throw e;  
            }  
        }  
        return list;  
    }  
      
    /\**  
     * csv文件做成&lt;BR/&gt; 将argList写入argPath路径下的argFileName文件里。  
     *   
     * @param argList  
     *            要写入csv文件的数据（List&lt;String\[]&gt;）  
     * @param argPath  
     *            csv文件路径  
     * @param argFileName  
     *            csv文件名  
     * @param isNewFile  
     *            是否覆盖原有文件  
     * @throws IOException  
     * @throws Exception  
     \*/  
    public static void writeCsvFile(List&lt;String\[]&gt; argList, String argPath,  
            String argFileName, boolean isNewFile) throws IOException,  
            Exception  
    {  
        CsvFileUtil util = new CsvFileUtil();  
        // 数据check  
        if (argList == null || argList.size() == 0)  
        {  
            throw new Exception("没有数据");  
        }  
        for (int i = 0; i &lt; argList.size(); i++)  
        {  
            if (!(argList.get(i) instanceof String\[]))  
            {  
                throw new Exception("数据格式不对");  
            }  
        }  
        FileWriter fileWriter = null;  
        BufferedWriter bufferedWriter = null;  
        String strFullFileName = argPath;  
        if (strFullFileName.lastIndexOf("\\\\") == (strFullFileName.length() - 1))  
        {  
            strFullFileName += argFileName;  
        }  
        else  
        {  
            strFullFileName += "\\\\" + argFileName;  
        }  
        File file = new File(strFullFileName);  
        // 文件路径check  
        if (!file.getParentFile().exists())  
        {  
            file.getParentFile().mkdirs();  
        }  
        try  
        {  
            if (isNewFile)  
            {  
                // 覆盖原有文件  
                fileWriter = new FileWriter(file);  
            }  
            else  
            {  
                // 在原有文件上追加数据  
                fileWriter = new FileWriter(file, true);  
            }  
            bufferedWriter = new BufferedWriter(fileWriter);  
            for (int i = 0; i &lt; argList.size(); i++)  
            {  
                String\[] strTemp = (String\[]) argList.get(i);  
                for (int j = 0; j &lt; strTemp.length; j++)  
                {  
                    if (util.isExisted("\\"", strTemp\[j]))  
                    {  
                        strTemp\[j] = strTemp\[j].replaceAll("\\"", "\\"\\"");  
                        bufferedWriter.write("\\"" + strTemp\[j] + "\\"");  
                    }  
                    else if (util.isExisted(",", strTemp\[j])  
                            || util.isExisted("\\n", strTemp\[j])  
                            || util.isExisted(" ", strTemp\[j])  
                            || util.isExisted("??", strTemp\[j]))  
                    {  
                        bufferedWriter.write("\\"" + strTemp\[j] + "\\"");  
                    }  
                    else  
                    {  
                        bufferedWriter.write(strTemp\[j]);  
                    }  
                    if (j &lt; strTemp.length - 1)  
                    {  
                        bufferedWriter.write(",");  
                    }  
                }  
                bufferedWriter.newLine();  
            }  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        finally  
        {  
            try  
            {  
                if (bufferedWriter != null)  
                {  
                    bufferedWriter.close();  
                }  
                if (fileWriter != null)  
                {  
                    fileWriter.close();  
                }  
            }  
            catch (IOException e)  
            {  
                throw e;  
            }  
        }  
    }  
      
    /\**  
     * @param argChar  
     * @param argStr  
     * @return  
     \*/  
    private boolean isExisted(String argChar, String argStr)  
    {  
          
        boolean blnReturnValue = false;  
        if ((argStr.indexOf(argChar) &gt;= 0)  
                && (argStr.indexOf(argChar) &lt;= argStr.length()))  
        {  
            blnReturnValue = true;  
        }  
        return blnReturnValue;  
    }  
      
    /\**  
     * 正则表达式。  
     *   
     * @return 匹配csv文件里最小单位的正则表达式。  
     \*/  
    private String getRegExp()  
    {  
        StringBuffer strRegExps = new StringBuffer();  
        strRegExps.append("\\"((");  
        strRegExps.append(SPECIAL\_CHAR\_A);  
        strRegExps.append("\*\[,\\\\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("\*\[ 　]\*,\[ 　]\*");  
        strRegExps.append("|\\"((");  
        strRegExps.append(SPECIAL\_CHAR\_A);  
        strRegExps.append("\*\[,\\\\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();  
    }  
      
    private static final String SPECIAL\_CHAR\_A = "\[^\\",\\\\n 　]";  
      
    private static final String SPECIAL\_CHAR\_B = "\[^\\",\\\\n]";  
}