Parsing Large CSV File Where I Only Need The Values in 2 Columns (Java)
I have the following part of a CSV File with 7 c开发者_Go百科olumns (see first line) and I want to put the dates (1st column) as the keys in a TreeMap and the Adj Close values (7th column) as the mapped values in the TreeMap:
Date,Open,High,Low,Close,Volume,Adj Close
7/1/2011,132.09,134.1,131.78,133.92,202370700,133.92 6/30/2011,131.14,132.18,130.71,131.97,223496600,131.97 6/29/2011,130.2,130.93,129.63,130.72,244295500,130.72 6/28/2011,128.45,129.63,128.27,129.61,165556300,129.61
In an earlier part of the assignment, I only had to put the Open values (2nd column) as the mapped values (the dates were the keys) in a TreeMap. I used Scanner for this and my code is below:
TreeMap<String, String> loadPriceData(String fileName) throws Exception
{
TreeMap<String, String> prices = new TreeMap<String, String>();//create prices map
Scanner fileScanner = new Scanner(new File(fileName));
fileScanner.useDelimiter("[,\n]+");// use comma as delimiter
while(fileScanner.hasNext()) //condition detects comma
{
prices.put(fileScanner.nextLine(),fileScanner.nextLine());
}
return prices;
}
But this seems only good for 2 column CSV data. If I need the mapped values in the 7th column, what's an efficient way to go about it? Thanks in advance.
Your code doesn't work. The delimiter pattern is not correct. If you look at the contents of your map, you will see that instead of having date-price mappings, you only have one strange mapping.
Instead of using a Scanner, a simpler way is to read the file line-by-line, split each line on comma and put the fields you need into the map.
For example:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<String, String>();// create prices map
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(fileName));
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {// ignore
}
}
}
}
If you are using Java 7, you can make use of the try-with-resources statement:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<>();// create prices map
try (BufferedReader in = Files.newBufferedReader(Paths.get(fileName}),
Charset.forName("UTF-8"))) {
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
}
}
精彩评论