gov.sandia.cognition.io
Class CSVUtility

java.lang.Object
  extended by gov.sandia.cognition.io.CSVUtility

@CodeReviews(reviews={@CodeReview(reviewer="Kevin R. Dixon",date="2006-07-18",changesNeeded=false,comments="Minor spacing changes to while statement. Otherwise, looks fine."),@CodeReview(reviewer="Kevin R. Dixon",date="2008-02-08",changesNeeded=true,comments="This class should make the comma a parameter (in the splitCommas() method) so that it can be used on all character-delimited parsing schemes.",response=@CodeReviewResponse(respondent="Justin Basilico",date="2008-02-18",moreChangesNeeded=false,comments="Added the split() method"))})
public class CSVUtility
extends Object

The CSVUtility class implements some utility functions for dealing with comma-separated value (CSV) file types.

Since:
1.0
Author:
Justin Basilico, Kevin R. Dixon

Constructor Summary
CSVUtility()
           
 
Method Summary
static String[] nextNonEmptyLine(BufferedReader r)
          Returns the next non-empty line from the given BufferedReader as an array of the CSV entries.
static String[] nextNonEmptyLine(BufferedReader r, char delimiter)
          Returns the next non-empty line from the given BufferedReader as an array of the CSV entries.
static LinkedList<String[]> readFile(String fileName)
          Reads a CSV file into a list of arrays of string values.
static LinkedList<String[]> readFile(String fileName, char delimiter)
          Reads a CSV file into a list of arrays of string values.
static String[] split(String line, char c)
          Splits the given line into the array of character-separated values.
static String[] splitCommas(String line)
          Splits the given line into the array of comma-separated values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVUtility

public CSVUtility()
Method Detail

nextNonEmptyLine

public static String[] nextNonEmptyLine(BufferedReader r)
                                 throws IOException
Returns the next non-empty line from the given BufferedReader as an array of the CSV entries. If there is no more data, null is returned.

Parameters:
r - The BufferedReader to read from.
Returns:
An array of the comma-separated values on the next line or null if the end of the file has been reached.
Throws:
IOException - If there is an exception in reading from the BufferedReader.

nextNonEmptyLine

public static String[] nextNonEmptyLine(BufferedReader r,
                                        char delimiter)
                                 throws IOException
Returns the next non-empty line from the given BufferedReader as an array of the CSV entries. If there is no more data, null is returned.

Parameters:
r - The BufferedReader to read from.
delimiter - Delimiter to use.
Returns:
An array of the comma-separated values on the next line or null if the end of the file has been reached.
Throws:
IOException - If there is an exception in reading from the BufferedReader.

splitCommas

public static String[] splitCommas(String line)
Splits the given line into the array of comma-separated values. If the given line is null, then the value returned is null. If there is no data on the line, then an empty array is returned. Otherwise, an array containing at least one value is returned.

Parameters:
line - The line to split on the commas.
Returns:
The array of comma-separated values from the given line.

split

public static String[] split(String line,
                             char c)
Splits the given line into the array of character-separated values. If the given line is null, then the value returned is null. If there is no data on the line, then an empty array is returned. Otherwise, an array containing at least one value is returned.

Parameters:
line - The line to split on the given character.
c - The character to use to split the line.
Returns:
The array of comma-separated values from the given line.

readFile

public static LinkedList<String[]> readFile(String fileName)
                                     throws IOException
Reads a CSV file into a list of arrays of string values.

Parameters:
fileName - The file to read in.
Returns:
A list of arrays of strings that contain the comma-separated values in the given CSV file.
Throws:
IOException - If there is an error reading the file.

readFile

public static LinkedList<String[]> readFile(String fileName,
                                            char delimiter)
                                     throws IOException
Reads a CSV file into a list of arrays of string values.

Parameters:
fileName - The file to read in.
delimiter - Delimiter to use.
Returns:
A list of arrays of strings that contain the comma-separated values in the given CSV file.
Throws:
IOException - If there is an error reading the file.