Creates CSV phrase from objects - Java File Path IO

Java examples for File Path IO:CSV File

Description

Creates CSV phrase from objects

Demo Code

/**/*from www  .  j av  a2s . c om*/
    JEM, the BEE - Job Entry Manager, the Batch Execution Environment
    Copyright (C) 2012-2015   Marco "Fuzzo" Cuccato
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.
            
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
            
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.util.Collection;

public class Main {
    /**
     * 
     */
    public static final char COMMA_CHAR = ',';
    /**
     * 
     */
    public static final String NULL = "null";

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @return a string, with each object separated by <code>,</code>
     */
    public static String getCSVPhrase(Collection<?> objects) {
        return getCSVPhrase(objects, COMMA_CHAR);
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the char to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Collection<?> objects, char separator) {
        return getCSVPhrase(objects, String.valueOf(separator));
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the string to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Collection<?> objects,
            String separator) {
        StringBuilder s = new StringBuilder();
        if (!objects.isEmpty()) {
            for (Object o : objects) {
                if (o != null) {
                    s.append(o.toString());
                } else {
                    s.append(NULL);
                }
                s.append(separator);
            }
            s.setLength(s.length() - separator.length());
        }
        return s.toString();
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @return a string, with each object separated by <code>,</code>
     */
    public static String getCSVPhrase(Object[] objects) {
        return getCSVPhrase(objects, COMMA_CHAR);
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the char to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Object[] objects, char separator) {
        return getCSVPhrase(objects, String.valueOf(separator));
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the string to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Object[] objects, String separator) {
        StringBuilder s = new StringBuilder();
        if (objects.length > 0) {
            for (Object o : objects) {
                if (o != null) {
                    s.append(o.toString());
                } else {
                    s.append(NULL);
                }
                s.append(separator);
            }
            s.setLength(s.length() - separator.length());
        }
        return s.toString();
    }
}

Related Tutorials