Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright 2010 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.api.ads.common.lib.utils;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* A utility class for processing and handling CSV files.
*/
public final class CsvFiles {
/**
* {@code CsvFiles} is meant to be used statically.
*/
private CsvFiles() {}
/**
* Returns a {@code Map} 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} 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 getCsvDataMap(String fileName,
final int key, final int value, boolean headerPresent) throws IOException {
final Map result = Maps.newHashMap();
new CsvReader(fileName, headerPresent)
.processReader((header, line, lineNumber) -> result.put(line[key], line[value]));
return result;
}
/**
* Returns a {@code Map} mapping of the first column to the
* second column. This method also ignores all columns other than the first
* two.
*
* @param fileName the CSV file to load
* @param headerPresent {@code true} if the fist line is the header
* @return a {@code Map} mapping of the first to the second
* column
* @throws IOException if there was an exception reading the file
* @throws IllegalArgumentException if CSV file has fewer than two
* columns
*/
public static Map getCsvDataMap(String fileName, boolean headerPresent)
throws IOException {
return getCsvDataMap(fileName, 0, 1, headerPresent);
}
/**
* Returns a {@code Map} 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} 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 getCsvDataMapArray(String fileName, boolean headerPresent)
throws IOException {
final Map 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 List