Java CSV from formatCsvRecord(List csvRecord, char csvSeparator)

Here you can find the source of formatCsvRecord(List csvRecord, char csvSeparator)

Description

CSV record formatter.

License

Open Source License

Parameter

Parameter Description
csvRecord A List of Objects representing the fields of a CSV reecord.
csvSeparator The CSV field separator to be used.

Return

A String representing a CSV record.

Declaration

private static <T extends Object> String formatCsvRecord(List<T> csvRecord, char csvSeparator) 

Method Source Code

//package com.java2s;
/*/*  ww  w .  j av  a  2  s  .co m*/
 * net/balusc/util/CsvUtil.java
 * 
 * Copyright (C) 2006 BalusC
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * CSV record formatter. Convert a List of Objects representing the fields of a CSV record to a
     * String representing the CSV record. The value of each Object will be obtained by its
     * toString() method. The fields of the CSV record will be separated by the specified CSV field
     * separator.
     * @param csvRecord A List of Objects representing the fields of a CSV reecord.
     * @param csvSeparator The CSV field separator to be used.
     * @return A String representing a CSV record.
     */
    private static <T extends Object> String formatCsvRecord(List<T> csvRecord, char csvSeparator) {

        // Prepare.
        StringBuilder fields = new StringBuilder();
        String separator = String.valueOf(csvSeparator);

        // Process fields.
        for (Iterator<T> iter = csvRecord.iterator(); iter.hasNext();) {
            T object = iter.next();

            if (object != null) {
                String field = object.toString().replace("\"", "\"\""); // Escape quotes.

                if (field.contains(separator) || field.contains("\"")) {
                    field = "\"" + field + "\""; // Surround with quotes.
                }

                fields.append(field);
            }

            if (iter.hasNext()) {
                fields.append(separator); // Add field separator.
            }
        }

        return fields.toString();
    }
}

Related

  1. csvTagsToList(String tags)
  2. csvToList(String csv)
  3. csvToList(String csv)
  4. CSVToList(String str)
  5. formatAsCsv(List stringList)
  6. getArrayListFromCSV(String csvString)
  7. getAsCsv(List tags)
  8. getCSV(List l)
  9. getCSVFromList(List myList)