/** * Returns a {@code 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. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the first line is the header * @return a {@code 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 */ public static List<Map<String, String>> getCsvDataListMap(String fileName, boolean headerPresent) throws IOException { final List<Map<String, String>> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader( (headers, line, lineNumber) -> { Map<String, String> data = Maps.newHashMap(); for (int i = 0; i < line.length; i++) { if (headers != null) { data.put(headers[i], line[i]); } else { data.put(i + "", line[i]); } } result.add(data); }); return result; }
/** * Returns a {@code Map<String, String[]>} mapping of the first column to an * array of the rest of the columns. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the fist line is the header * @return a {@code 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 * @throws IOException if there was an exception reading the file */ public static Map<String, String[]> getCsvDataMapArray(String fileName, boolean headerPresent) throws IOException { final Map<String, String[]> result = Maps.newHashMap(); new CsvReader(fileName, headerPresent) .processReader( (header, line, lineNumber) -> result.put( line[0], Arrays.asList(line) .subList(1, line.length) .toArray(new String[line.length - 1]))); return result; }
/** * Returns a {@code Map<String, String>} mapping of the column designated by * {@code key} to the column designated by {@code value}. This method also * ignores all columns other than the columns specified by {@code key} and * {@code value}. * * @param fileName the CSV file to load * @param key the 0-indexed column number to map to the key of the returned * data map * @param value the column number to map to the value of the returned data * map * @param headerPresent {@code true} if the fist line is the header * @return a {@code Map<String, String>} mapping of the columns specified by * {@code key} and {@code value} * @throws IOException if there was an error while reading the file * @throws IllegalArgumentException if CSV file does not have the * columns specified by {@code key} or {@code value} */ public static Map<String, String> getCsvDataMap(String fileName, final int key, final int value, boolean headerPresent) throws IOException { final Map<String, String> result = Maps.newHashMap(); new CsvReader(fileName, headerPresent) .processReader((header, line, lineNumber) -> result.put(line[key], line[value])); return result; }
/** * Returns a {@code 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. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the first line is the header * @return a {@code 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 */ public static List<Map<String, String>> getCsvDataListMap(String fileName, boolean headerPresent) throws IOException { final List<Map<String, String>> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader( (headers, line, lineNumber) -> { Map<String, String> data = Maps.newHashMap(); for (int i = 0; i < line.length; i++) { if (headers != null) { data.put(headers[i], line[i]); } else { data.put(i + "", line[i]); } } result.add(data); }); return result; }
/** * Returns a {@code Map<String, String>} mapping of the column designated by * {@code key} to the column designated by {@code value}. This method also * ignores all columns other than the columns specified by {@code key} and * {@code value}. * * @param fileName the CSV file to load * @param key the 0-indexed column number to map to the key of the returned * data map * @param value the column number to map to the value of the returned data * map * @param headerPresent {@code true} if the fist line is the header * @return a {@code Map<String, String>} mapping of the columns specified by * {@code key} and {@code value} * @throws IOException if there was an error while reading the file * @throws IllegalArgumentException if CSV file does not have the * columns specified by {@code key} or {@code value} */ public static Map<String, String> getCsvDataMap(String fileName, final int key, final int value, boolean headerPresent) throws IOException { final Map<String, String> result = Maps.newHashMap(); new CsvReader(fileName, headerPresent) .processReader((header, line, lineNumber) -> result.put(line[key], line[value])); return result; }
/** * Returns a {@code Map<String, String[]>} mapping of the first column to an * array of the rest of the columns. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the fist line is the header * @return a {@code 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 * @throws IOException if there was an exception reading the file */ public static Map<String, String[]> getCsvDataMapArray(String fileName, boolean headerPresent) throws IOException { final Map<String, String[]> result = Maps.newHashMap(); new CsvReader(fileName, headerPresent) .processReader( (header, line, lineNumber) -> result.put( line[0], Arrays.asList(line) .subList(1, line.length) .toArray(new String[line.length - 1]))); return result; }
/** * Performs the {@link CsvWorker#processLine(String[], String[], int)} * method of the {@code worker} parameter for each link in the CSV, * and closes the underlying {@link CSVReader}. * * @param worker the {@code CsvWorker} that performs work on each line * @throws IOException if the CSV file cannot be read */ public void processReader(CsvWorker worker) throws IOException { createCsvReader(); try { String[] line; while ((line = reader.readNext()) != null) { worker.processLine(header, line, lineNumber); lineNumber++; } } finally { reader.close(); } }
/** * Returns a {@code List<String>} representing a single 0-indexed column. * * @param fileName the CSV file to load * @param column the 0-indexed column to return * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String>} representing a single column * @throws IOException if there was an exception reading the file * @throws IllegalArgumentException if the column index does not exist in * the CSV */ public static List<String> getCsvDataByColumn(String fileName, final int column, boolean headerPresent) throws IOException { final List<String> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader((headers, line, lineNumber) -> result.add(line[column])); return result; }
/** * Returns a {@code List<String>} representing a single 0-indexed column. * * @param fileName the CSV file to load * @param column the 0-indexed column to return * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String>} representing a single column * @throws IOException if there was an exception reading the file * @throws IllegalArgumentException if the column index does not exist in * the CSV */ public static List<String> getCsvDataByColumn(String fileName, final int column, boolean headerPresent) throws IOException { final List<String> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader((headers, line, lineNumber) -> result.add(line[column])); return result; }
/** * Performs the {@link CsvWorker#processLine(String[], String[], int)} * method of the {@code worker} parameter for each link in the CSV, * and closes the underlying {@link CSVReader}. * * @param worker the {@code CsvWorker} that performs work on each line * @throws IOException if the CSV file cannot be read */ public void processReader(CsvWorker worker) throws IOException { createCsvReader(); try { String[] line; while ((line = reader.readNext()) != null) { worker.processLine(header, line, lineNumber); lineNumber++; } } finally { reader.close(); } }
/** * Returns a {@code List<String[]>} that contains all rows of the CSV file. * The header will be removed, if present. * * @param csvReader reader used to read the csv * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String[]>} that contains all rows of the CSV file * @throws IOException if there was an exception reading the file */ public static List<String[]> getCsvDataArray(Reader csvReader, boolean headerPresent) throws IOException { final List<String[]> result = Lists.newArrayList(); new CsvReader(new CSVReader(csvReader), headerPresent) .processReader((headers, line, lineNumber) -> result.add(line)); return result; } /**
/** * Returns a {@code List<String[]>} that contains all rows of the CSV file. * The header will be removed, if present. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String[]>} that contains all rows of the CSV file * @throws IOException if there was an exception reading the file */ public static List<String[]> getCsvDataArray(String fileName, boolean headerPresent) throws IOException { final List<String[]> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader((headers, line, lineNumber) -> result.add(line)); return result; }
/** * Returns a {@code List<String[]>} that contains all rows of the CSV file. * The header will be removed, if present. * * @param csvReader reader used to read the csv * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String[]>} that contains all rows of the CSV file * @throws IOException if there was an exception reading the file */ public static List<String[]> getCsvDataArray(Reader csvReader, boolean headerPresent) throws IOException { final List<String[]> result = Lists.newArrayList(); new CsvReader(new CSVReader(csvReader), headerPresent) .processReader((headers, line, lineNumber) -> result.add(line)); return result; } /**
/** * Returns a {@code List<String[]>} that contains all rows of the CSV file. * The header will be removed, if present. * * @param fileName the CSV file to load * @param headerPresent {@code true} if the first line is the header * @return a {@code List<String[]>} that contains all rows of the CSV file * @throws IOException if there was an exception reading the file */ public static List<String[]> getCsvDataArray(String fileName, boolean headerPresent) throws IOException { final List<String[]> result = Lists.newArrayList(); new CsvReader(fileName, headerPresent) .processReader((headers, line, lineNumber) -> result.add(line)); return result; }