All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.TKnudsen.ComplexDataObject.model.io.csv.CSVWriterTools Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

The newest version!
package com.github.TKnudsen.ComplexDataObject.model.io.csv;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 

* Description: support functionality to facilitate .csv output * * Possible add on: add encapsulation of special chars and comma *

* *

* Copyright: Copyright (c) 2018-2021 *

* * @author Juergen Bernard * @version 1.02 */ public class CSVWriterTools { private static final char SEPARATOR = ','; public static void writeToCSV(Iterable> linesOfObjects, String file) throws IOException { writeToCSV(linesOfObjects, file, SEPARATOR); } public static void writeToCSV(Iterable> linesOfObjects, String file, char separator) throws IOException { Writer writer = createFileWriter(file); for (Iterable objects : linesOfObjects) writeLine(writer, objects, separator); writer.flush(); writer.close(); } public static void writeLine(Writer writer, Iterable values) throws IOException { writeLine(writer, values, SEPARATOR); } public static void writeLine(Writer writer, Iterable values, char separator) throws IOException { boolean first = true; if (separator == ' ') separator = SEPARATOR; StringBuilder sb = new StringBuilder(); for (Object value : values) { if (!first) sb.append(separator); sb.append(csvFormatSupport(value.toString())); first = false; } sb.append("\n"); writer.append(sb.toString()); } private static String csvFormatSupport(String value) { String output = value; return output.replace("\"", "\"\""); } public static Writer createFileWriter(String csvFile) throws IOException { return new FileWriter(csvFile); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy