casmi.io.parser.CSV Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of casmi-io Show documentation
Show all versions of casmi-io Show documentation
casmi extension module for I/O
The newest version!
/*
* casmi
* http://casmi.github.com/
* Copyright (C) 2011, Xcoo, Inc.
*
* casmi 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 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package casmi.io.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
/**
* CSV parser class.
*
*
* This class uses opencsv-2.1
* library (licensed by Apache 2.0).
*
*
*
* If you want to parse other separated files (e.g. TSV file), you can also use
* this CSV class. Use a constructor "CSV(String pathname, char separator)"
* like,
*
*
* CSV tsv = new CSV("file.tsv", "\t");
*
*
*
* @author T. Takeuchi
*
*/
public class CSV {
/** Default separator. */
private static final char DEFAULT_SEPARATOR = ',';
/** A File object. */
private final File file;
/** A separator. */
private char separator = DEFAULT_SEPARATOR;
/**
* CSVReader object in opencsv library.
*/
private CSVReader csvReader = null;
/**
* CSVWriter object. in opencsv library.
*/
private CSVWriter csvWriter = null;
// -------------------------------------------------------------------------
// Constructors.
// -------------------------------------------------------------------------
/**
* Creates CSV object from a File.
*
* @param file a File object of a csv file
*/
public CSV(File file) {
this(file, DEFAULT_SEPARATOR);
}
/**
* Creates CSV object from a File.
* Second argument is a character to separate each element.
*
* @param file a File object of a csv file
*/
public CSV(File file, char separator) {
this.file = file;
this.separator = separator;
}
/**
* Creates CSV object from a file path string.
*
* @param pathname
* a file path string of a csv file
*/
public CSV(String pathname) {
this(pathname, DEFAULT_SEPARATOR);
}
/**
* Creates CSV object from a file path URL.
*
* @param url
* URL of a csv file
*/
public CSV(URL url) {
this(new File( url2Uri(url) ), DEFAULT_SEPARATOR);
}
/**
* Creates CSV object from a file path string.
* Second argument is a character to separate each element.
*
* @param pathname
* a file path string of a csv file
*
* @param separator
* separator character
*/
public CSV(String pathname, char separator) {
this(new File(pathname), separator);
}
// -------------------------------------------------------------------------
// Getters and setters.
// -------------------------------------------------------------------------
/**
* Returns the character to separate each element.
*
* @return the separator char
.
*/
public char getSeparator() {
return separator;
}
// -------------------------------------------------------------------------
// Other methods.
// -------------------------------------------------------------------------
/**
* Reads the next line from the buffer and converts to a string array.
*
* @return
* a string array with each comma-separated element as a separate
* entry
*
* @throws IOException
* if bad things happen during the read
*/
public String[] readLine() throws IOException {
openReader();
return csvReader.readNext();
}
/**
* Reads the entire file into a List with each element being a String[] of
* tokens.
*
* @return
* a List of String[], with each String[] representing a line of the
* file
*
* @throws IOException
* if bad things happen during the read
*/
public java.util.List readAll() throws IOException {
openReader();
return csvReader.readAll();
}
/**
* Writes the next line to the file.
*
* @param elements
* element strings or a string array with each comma-separated
* element
* as a separate entry
*
* @throws IOException
* if an I/O error occurs
*/
public void writeLine(String... elements) throws IOException {
openWriter();
csvWriter.writeNext(elements);
}
/**
* Writes the entire list to a CSV file.
* The list is assumed to be a String[].
*
* @param allLines
* a List of String[], with each String[] representing a line of
* the file.
*
* @throws IOException
* if an I/O error occurs
*/
public void writeAll(java.util.List allLines) throws IOException {
openWriter();
csvWriter.writeAll(allLines);
}
private void openReader() throws FileNotFoundException {
if (csvReader == null) {
csvReader = new CSVReader(new java.io.FileReader(file), separator);
}
}
private void openWriter() throws IOException {
if (csvWriter == null) {
csvWriter = new CSVWriter(new java.io.FileWriter(file), separator);
}
}
/**
* Close reader and writer objects.
*/
public void close() {
if (csvReader != null) {
try {
csvReader.close();
} catch (IOException e) {
// Ignore.
}
}
if (csvWriter != null) {
try {
csvWriter.close();
} catch (IOException e) {
// Ignore.
}
}
}
private static URI url2Uri(URL url) {
try {
return new URI(url.toString());
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
}