至CSV行

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

525
到CSV行


//package com.nowjava;


import java.util.ArrayList;
/*
 from NowJava.com - 时代Java 
*/


public class Main {


    public static String toCSVLine(String[] strArray) {

        if (strArray == null) {

            return "";

        }

        StringBuffer cvsLine = new StringBuffer();
        /*
        N o w  J a v a  .   c o m 提供
        */

        for (int idx = 0; idx < strArray.length; idx++) {

            String item = addQuote(strArray[idx]);

            cvsLine.append(item);

            if (strArray.length - 1 != idx) {

                cvsLine.append(',');

            }

        }

        return cvsLine.toString();

    }


    public static String toCSVLine(ArrayList<?> strArrList) {

        if (strArrList == null) {

            return "";

        }

        String[] strArray = new String[strArrList.size()];

        for (int idx = 0; idx < strArrList.size(); idx++) {

            strArray[idx] = (String) strArrList.get(idx);

        }

        return toCSVLine(strArray);

    }


    private static String addQuote(String item) {

        if (item == null || item.length() == 0) {

            return "\"\"";

        }

        StringBuffer sb = new StringBuffer();

        sb.append('"');

        for (int idx = 0; idx < item.length(); idx++) {

            
展开阅读全文