集册 Java实例教程 CSV内容分析器。

CSV内容分析器。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

507
CSV内容解析器。


//package com.nowjava;/*from n o w    j a v a  . c o m*/

import java.io.*;

import java.util.ArrayList;


import java.util.List;


public class Main {

    private static final char DEFAULT_CSV_SEPARATOR = ';';


    /**

     * CSV content parser. Convert an InputStream with the CSV contents to a two-dimensional List

     * of Strings representing the rows and columns of the CSV. Each CSV record is expected to be

     * separated by the default CSV field separator, a comma.

     *

     * @param csvInput The InputStream with the CSV contents.

     * @return A two-dimensional List of Strings representing the rows and columns of the CSV.

     */

    public static List<List<String>> parseCsv(InputStream csvInput) {

        return parseCsv(csvInput, DEFAULT_CSV_SEPARATOR);

    }


    /**

     * CSV content parser. Convert an InputStream with the CSV contents to a two-dimensional List

     * of Strings representing the rows and columns of the CSV. Each CSV record is expected to be

     * separated by the specified CSV field separator.

     *

     * @param csvInput     The InputStream with the CSV contents.

     * @param csvSeparator The CSV field separator to be used.

     * @return A two-dimensional List of Strings representing the rows and columns of the CSV.

     */

    public static List<List<String>> parseCsv(InputStream csvInput,

            char csvSeparator) {


        // Prepare.

        BufferedReader csvReader = null;
        /**来自 
         N o w  J a v a  .   c o m**/

        List<List<String>> csvList = new ArrayList<List<String>>();

        String csvRecord = null;


        // Process records.

        try {

            csvReader = new BufferedReader(new InputStreamReader(csvInput,

                    "UTF-8"));

            while ((csvRecord = csvReader.readLine()) != null) {

                csvList.add(parseCsvRecord(csvRecord, csvSeparator));

            }

        } catch (IOException e) {

            throw new RuntimeException("Reading CSV failed.", e);

        } finally {

            if (csvReader != null)

                try {

                    csvReader.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

        }


        return csvList;

    }


    /**

     * CSV record parser. Convert a CSV record to a List of Strings representing the fields of the

     * CSV record. The CSV record is expected to be separated by the specified CSV field separator.

     *

     * @param record       The CSV record.

     * @param csvSeparator The CSV field separator to be used.

     * @return A List of Strings representing the fields of each CSV record.

     */

    private static List<String> parseCsvRecord(String record,

            char csvSeparator) {


        // Prepare.

        boolean quoted = false;

        StringBuilder fieldBuilder = new StringBuilder();

        List<String> fields = new ArrayList<String>();


        // Process fields.

        for (int i = 0; i < record.length(); i++) {

            char c = record.charAt(i);

            fieldBuilder.append(c);


            if (c == '"') {

                quoted = !quoted; // Detect nested quotes.

            }


            if ((!quoted && c == csvSeparator) // The separator ..

                    || i + 1 == record.length()) // .. or, the end of record.

        
展开阅读全文