package com.cv.csv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author Chandra Vardhan
*
*/
public class CsvReader {
public static List<Map<String, String>> readSpecifiedColumns(final String absoluteFilePathString,
final int... retrieveColumns) {
Map<Integer, String[]> allRows = readCSVFile(absoluteFilePathString);
ArrayList<Map<String, String>> fileInfo = new ArrayList<Map<String, String>>();
for (Entry<Integer, String[]> hashMap : allRows.entrySet()) {
String[] row = hashMap.getValue();
Map<String, String> tempMap = new HashMap<String, String>();
if (row != null && row.length > 0) {
if (retrieveColumns != null && retrieveColumns.length > 0) {
for (int columnInt : retrieveColumns) {
tempMap.put("" + columnInt, row[columnInt]);
}
} else {
for (int j = 0; j < row.length; ++j) {
tempMap.put("" + j, row[j]);
}
}
}
fileInfo.add(tempMap);
tempMap = null;
}
allRows = null;
return fileInfo;
}
public static List<Map<String, String>> readSpecifiedColumns(final String absoluteFilePathString,
final String... retrieveColumns) {
Map<Integer, String[]> allRows = readCSVFile(absoluteFilePathString);
ArrayList<Map<String, String>> fileInfo = new ArrayList<Map<String, String>>();
int[] retrieveValues = new int[retrieveColumns.length];
boolean flag = false;
for (Entry<Integer, String[]> hashMap : allRows.entrySet()) {
Integer key = hashMap.getKey();
String[] row = hashMap.getValue();
if (key == 0) {
int retrieveRow = 0;
for (int i = 0; i < row.length; i++) {
String string2 = row[i];
for (int k = 0; k < retrieveColumns.length; ++k) {
String column = retrieveColumns[k];
if (column != null && column.equalsIgnoreCase(string2)) {
flag = true;
retrieveValues[retrieveRow] = i;
++retrieveRow;
}
}
}
continue;
}
Map<String, String> tempMap = new HashMap<String, String>();
if(flag) {
if (row != null && row.length > 0) {
if (retrieveValues != null && retrieveValues.length > 0) {
for (int columnInt : retrieveValues) {
tempMap.put("" + columnInt, row[columnInt]);
}
} else {
for (int j = 0; j < row.length; ++j) {
tempMap.put("" + j, row[j]);
}
}
}
fileInfo.add(tempMap);
}
tempMap = null;
}
allRows = null;
return fileInfo;
}
public static Map<Integer, String[]> readSpecifiedRows(final String absoluteFilePathString,
final int... retrieveRows) {
Map<Integer, String[]> allRows = readCSVFile(absoluteFilePathString);
Map<Integer, String[]> fileInfo = new HashMap<Integer, String[]>();
for (int retrieveRow : retrieveRows) {
String[] row = allRows.get(retrieveRow);
if (row != null && row.length > 0) {
fileInfo.put(retrieveRow, row);
}
}
allRows = null;
return fileInfo;
}
public static Map<Integer, String[]> readCSVFile(final String absoluteFilePathString) {
BufferedReader fileReader = null;
// Delimiter used in CSV file
String DELIMITER = ",";
Map<Integer, String[]> map = new HashMap<Integer, String[]>();
try {
String line = "";
// Create the file reader
fileReader = new BufferedReader(new FileReader(absoluteFilePathString));
int count = 0;
// Read the file line by line
while ((line = fileReader.readLine()) != null) {
if (count == 0) {
if (line.contains("\",\"")) {
String[] countMatches = line.split("\",\"");
if (countMatches.length > 1) {
DELIMITER = "\",\"";
}
}
if (line.contains("\t")) {
String[] countMatches = line.split("\t");
String[] countMatchesComma = line.split(",");
if (countMatches.length > 1 && countMatchesComma.length <= 1) {
DELIMITER = "\t";
}
}
}
String[] values = line.split(DELIMITER);
if (values != null && values.length > 1) {
String first = values[0];
if (first != null && first.startsWith("\"")) {
values[0] = first.substring(1, first.length());
}
String last = values[values.length - 1];
if (last != null && last.endsWith("\"")) {
values[values.length - 1] = last.substring(0, last.length() - 1);
}
map.put(count, values);
}
++count;
}
} catch (Exception e) {
System.err.println("Specified file " + absoluteFilePathString
+ " is not available / unable to read. Please provide absolute file path.");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
public static List<String[]> readCSVFileList(final String absoluteFilePathString) {
BufferedReader fileReader = null;
// Delimiter used in CSV file
String DELIMITER = ",";
List<String[]> list = new ArrayList<String[]>();
try {
String line = "";
// Create the file reader
fileReader = new BufferedReader(new FileReader(absoluteFilePathString));
int count = 0;
// Read the file line by line
while ((line = fileReader.readLine()) != null) {
if (count == 0) {
if (line.contains("\",\"")) {
String[] countMatches = line.split("\",\"");
if (countMatches.length > 1) {
DELIMITER = "\",\"";
}
}
if (line.contains("\t")) {
String[] countMatches = line.split("\t");
String[] countMatchesComma = line.split(",");
if (countMatches.length > 1 && countMatchesComma.length <= 1) {
DELIMITER = "\t";
}
}
}
String[] values = line.split(DELIMITER);
if (values != null && values.length > 1) {
String first = values[0];
if (first != null && first.startsWith("\"")) {
values[0] = first.substring(1, first.length());
}
String last = values[values.length - 1];
if (last != null && last.endsWith("\"")) {
values[values.length - 1] = last.substring(0, last.length() - 1);
}
list.add(values);
}
++count;
}
} catch (Exception e) {
System.err.println("Specified file " + absoluteFilePathString
+ " is not available / unable to read. Please provide absolute file path.");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
public static Map<String, Map<String, String>> readSpecifiedRowsSpecifiedColumns(
final String absoluteFilePathString, final int[] retrieveRows, final int... retriveColumns) {
Map<Integer, String[]> allRows = readCSVFile(absoluteFilePathString);
Map<String, Map<String, String>> fileInfo = new HashMap<String, Map<String, String>>();
for (int retrieveRow : retrieveRows) {
String[] row = allRows.get(retrieveRow);
Map<String, String> tempMap = new HashMap<String, String>();
if (row != null && row.length > 0) {
if (retriveColumns != null && retriveColumns.length > 0) {
for (int columnInt : retriveColumns) {
tempMap.put("" + columnInt, row[columnInt]);
}
} else {
for (int j = 0; j < row.length; ++j) {
tempMap.put("" + j, row[j]);
}
}
}
fileInfo.put("" + retrieveRow, tempMap);
tempMap = null;
}
allRows = null;
return fileInfo;
}
public static Map<String, Map<String, String>> readSpecifiedRowSpecifiedColumn(final String absoluteFilePathString,
final int retrieveRow, final int... retrieveColumns) {
Map<Integer, String[]> allRows = readCSVFile(absoluteFilePathString);
Map<String, Map<String, String>> fileInfo = new HashMap<String, Map<String, String>>();
String[] row = allRows.get(retrieveRow);
Map<String, String> tempMap = new HashMap<String, String>();
if (row != null && row.length > 0) {
if (retrieveColumns != null && retrieveColumns.length > 0) {
for (int columnInt : retrieveColumns) {
tempMap.put("" + columnInt, row[columnInt]);
}
} else {
for (int j = 0; j < row.length; ++j) {
tempMap.put("" + j, row[j]);
}
}
}
fileInfo.put("" + retrieveRow, tempMap);
row = null;
tempMap = null;
allRows = null;
return fileInfo;
}
} |
No comments:
Post a Comment