
org.diirt.graphene.profile.io.CSVWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphene-profile Show documentation
Show all versions of graphene-profile Show documentation
Tools to profile the graph library.
/**
* Copyright (C) 2010-14 diirt developers. See COPYRIGHT.TXT
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
*/
package org.diirt.graphene.profile.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Handles writing data to a .CSV file.
*
* @author asbarber
*/
public final class CSVWriter {
/**
* Quote delimiter for a .CSV formatted output file.
*/
public static final String QUOTE = "\"";
/**
* Comma delimiter for a .CSV formatted output file.
*/
public static final String DELIM = ",";
/**
* Prevents instantiation.
*/
private CSVWriter(){}
//File Creation
//--------------------------------------------------------------------------
/**
* Creates a CSV file with the specified name.
* Will overwrite an existing file!
*
* @param filename path and name of file
* @return created CSV file
*/
public static File createNewFile(String filename){
try {
File outputFile = new File(filename + ".csv");
//Creates File
outputFile.createNewFile();
return outputFile;
} catch (IOException ex) {
Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
/**
* Creates a CSV file with the specified name.
* Will not overwrite an existing file!
* Instead a unique name will be found by appending .# to the original name.
*
* @param filename path and name of file
* @return created CSV file
*/
public static File createFile(String filename){
try {
File outputFile = new File(filename + ".csv");
//Prevent File Overwrite
int tmp = 1;
while (outputFile.exists()){
outputFile = new File(filename + ".csv" + "." + tmp);
tmp++;
}
//Creates File
outputFile.createNewFile();
return outputFile;
} catch (IOException ex) {
Logger.getLogger(CSVWriter.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
//--------------------------------------------------------------------------
//Data Output
//--------------------------------------------------------------------------
/**
* Writes each row as a set of entries in the .CSV file and
* outputs a new line between each row.
* Essentially, each row contains items that represent the column
* entries within that row.
* @param csvFile file to write to
* @param rows cells to write
*/
public static void writeData(File csvFile, List rows){
//Invalid File
if (csvFile == null){
throw new IllegalArgumentException("Cannot write to a null file.");
}
//Invalid data
if (rows == null){
throw new IllegalArgumentException("Must have non-null data to write.");
}
//Writes all rows
for (List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy