com.google.api.ads.common.lib.utils
Class CsvFiles

java.lang.Object
  extended by com.google.api.ads.common.lib.utils.CsvFiles

public final class CsvFiles
extends Object

A utility class for processing and handling CSV files.

Author:
Adam Rogal

Method Summary
static List<String[]> getCsvDataArray(Reader csvReader, boolean headerPresent)
          Returns a List<String[]> that contains all rows of the CSV file.
static List<String[]> getCsvDataArray(String fileName, boolean headerPresent)
          Returns a List<String[]> that contains all rows of the CSV file.
static List<String> getCsvDataByColumn(String fileName, int column, boolean headerPresent)
          Returns a List<String> representing a single 0-indexed column.
static List<Map<String,String>> getCsvDataListMap(String fileName, boolean headerPresent)
          Returns a List<Map<String, String>> that contains all rows with a field mapping defined by the header.
static Map<String,String> getCsvDataMap(String fileName, boolean headerPresent)
          Returns a Map<String, String> mapping of the first column to the second column.
static Map<String,String> getCsvDataMap(String fileName, int key, int value, boolean headerPresent)
          Returns a Map<String, String> mapping of the column designated by key to the column designated by value.
static Map<String,String[]> getCsvDataMapArray(String fileName, boolean headerPresent)
          Returns a Map<String, String[]> mapping of the first column to an array of the rest of the columns.
static void writeCsv(List<String[]> csvData, String fileName)
          Writes the CSV data located in csvData to the file located at fileName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getCsvDataMap

public static Map<String,String> getCsvDataMap(String fileName,
                                               int key,
                                               int value,
                                               boolean headerPresent)
                                        throws IOException
Returns a Map<String, String> mapping of the column designated by key to the column designated by value. This method also ignores all columns other than the columns specified by key and value.

Parameters:
fileName - the CSV file to load
key - the 0-indexed column number to map to the key of the returned data map
value - the column number to map to the value of the returned data map
headerPresent - true if the fist line is the header
Returns:
a Map<String, String> mapping of the columns specified by key and value
Throws:
IOException - if there was an error while reading the file
IllegalArgumentException - if CSV file does not have the columns specified by key or value

getCsvDataMap

public static Map<String,String> getCsvDataMap(String fileName,
                                               boolean headerPresent)
                                        throws IOException
Returns a Map<String, String> mapping of the first column to the second column. This method also ignores all columns other than the first two.

Parameters:
fileName - the CSV file to load
headerPresent - true if the fist line is the header
Returns:
a Map<String, String> mapping of the first to the second column
Throws:
IOException - if there was an exception reading the file
IllegalArgumentException - if CSV file has fewer than two columns

getCsvDataMapArray

public static Map<String,String[]> getCsvDataMapArray(String fileName,
                                                      boolean headerPresent)
                                               throws IOException
Returns a Map<String, String[]> mapping of the first column to an array of the rest of the columns.

Parameters:
fileName - the CSV file to load
headerPresent - true if the fist line is the header
Returns:
a Map<String, String[]> mapping of the first column to an array of the rest of the columns
Throws:
IllegalArgumentException - if there is fewer than 2 columns in the CSV
IOException - if there was an exception reading the file

getCsvDataListMap

public static List<Map<String,String>> getCsvDataListMap(String fileName,
                                                         boolean headerPresent)
                                                  throws IOException
Returns a List<Map<String, String>> that contains all rows with a field mapping defined by the header. If no header is present, then each field is the 0-indexed column number.

Parameters:
fileName - the CSV file to load
headerPresent - true if the first line is the header
Returns:
a List<Map<String, String>> that contains all rows with with a field mapping defined by the header if present.
Throws:
IOException - if there was an exception reading the file

getCsvDataByColumn

public static List<String> getCsvDataByColumn(String fileName,
                                              int column,
                                              boolean headerPresent)
                                       throws IOException
Returns a List<String> representing a single 0-indexed column.

Parameters:
fileName - the CSV file to load
column - the 0-indexed column to return
headerPresent - true if the first line is the header
Returns:
a List<String> representing a single column
Throws:
IOException - if there was an exception reading the file
IllegalArgumentException - if the column index does not exist in the CSV

getCsvDataArray

public static List<String[]> getCsvDataArray(String fileName,
                                             boolean headerPresent)
                                      throws IOException
Returns a List<String[]> that contains all rows of the CSV file. The header will be removed, if present.

Parameters:
fileName - the CSV file to load
headerPresent - true if the first line is the header
Returns:
a List<String[]> that contains all rows of the CSV file
Throws:
IOException - if there was an exception reading the file

getCsvDataArray

public static List<String[]> getCsvDataArray(Reader csvReader,
                                             boolean headerPresent)
                                      throws IOException
Returns a List<String[]> that contains all rows of the CSV file. The header will be removed, if present.

Parameters:
csvReader - reader used to read the csv
headerPresent - true if the first line is the header
Returns:
a List<String[]> that contains all rows of the CSV file
Throws:
IOException - if there was an exception reading the file

writeCsv

public static void writeCsv(List<String[]> csvData,
                            String fileName)
                     throws IOException
Writes the CSV data located in csvData to the file located at fileName.

Parameters:
csvData - the CSV data including the header
fileName - the file to write the CSV data to
Throws:
IOException - if there was an error writing to the file
IllegalArgumentException - if csvData == null


Copyright © 2012. All Rights Reserved.