集册 Java实例教程 从给定的值和分隔符创建CSV格式的行。

从给定的值和分隔符创建CSV格式的行。

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

789
根据给定的值和分隔符以CSV格式创建一行。
/*
来 自*
 nowjava.com
*/

/*******************************************************************************

 * Copyright (c) 2008 Actuate Corporation.

 * All rights reserved. This program and the accompanying materials

 * are made available under the terms of the Eclipse Public License v1.0

 * which accompanies this distribution, and is available at

 * http://www.eclipse.org/legal/epl-v10.html

 *

 * Contributors:

 *  Actuate Corporation  - initial API and implementation

 *******************************************************************************/


public class Main{

    public static final String CR = "\r";

    public static final String LF = "\n";

    public static final String QUOTE = "\"";

    /**

     * Creates a row in CSV format from the given values and separator.

     * The returned row includes the newline character.

     * @param values values of the columns

     * @param sep separator to use

     * @return CSV-formatted row using the given separator and values

     */

    public static String makeCSVRow(String[] values, String sep,

            boolean addCR) {

        StringBuffer buf = new StringBuffer(values.length * 10);

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

            if (i > 0) {

                buf.append(sep);

            }


            String value = quoteCSVValue(values[i], sep);

            if (value != null) {

                buf.append(value);
                /**
                N o w J a v a . c o m - 时代Java 提供 
                **/

            }

        }

        if (addCR) {

            buf.append(CR);

        }

        buf.append(LF);

        return buf.toString();

    }

    /**

     * Quotes a value in CSV format converter. Here are the rules:

     * <ol><li>Fields with given separator must be delimited with double-quote

     * characters.</li>

     * <li>Fields that contain double quote characters must be

     * surrounded by double-quotes, and the embedded double-quotes must each be

     * represented by a pair of consecutive double quotes.</li>

     * <li>A field that contains embedded line-breaks must be surrounded by

     * double-quotes.</li>

     * <li>Fields with leading or trailing spaces must be delimited with

     * double-quote characters.</li>

     * <li>Null values are represented by empty strings without quotes</li>

     * 

     * @param value value to quote

     * @param sep CSV separator, to check whether the value contains it

     * @return the value quoted in CSV format

     */

    public static String quoteCSVValue(String value, String sep) {

        if (value == null) {

            return null;

        } else if (value.length() == 0) {

            return QUOTE + QUOTE;

        }


        // escape quotes

        value = value.replaceAll(QUOTE, QUOTE + QUOTE);


        boolean needQuote = false;

        need
展开阅读全文