An easier way to parse a CSV file

I personally use the opencsv library to parse csv (comma separated value) files. It is a simple and powerful library with a commercial-friendly license. You can use it to directly parse a CSV file, or use its JavaBean binding feature. I prefer to use the JavaBean binding approach as it removes the mundane work of converting text to Java native types.

Below is an example of how to use JavaBeans binding feature of the opencsv library.

I will parse the historical stock prices of Apple Inc. downloaded from Yahoo! Finance.

First, I need to create a JavaBean that will represent and capture the data from each row of the csv file. Notice that I define my properties as double and long types. The opencsv library will automatically convert the text to the appropriate types.

public class StockQuote {
    double closePrice, openPrice, high, low;
    long volume;
    String date;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public double getClosePrice() {
        return closePrice;
    }

    public void setClosePrice(double closePrice) {
        this.closePrice = closePrice;
    }

    public double getOpenPrice() {
        return openPrice;
    }

    public void setOpenPrice(double openPrice) {
        this.openPrice = openPrice;
    }

    public double getHigh() {
        return high;
    }

    public void setHigh(double high) {
        this.high = high;
    }

    public double getLow() {
        return low;
    }

    public void setLow(double low) {
        this.low = low;
    }

    public long getVolume() {
        return volume;
    }

    public void setVolume(long volume) {
        this.volume = volume;
    }
}

Second, need to create a mapping of the columns to properties of my StockPrice class.

Hashtable<String, String> mapping = new Hashtable<String, String>();
mapping.put("Date", "date");
mapping.put("Close", "closePrice");
mapping.put("High", "high");
mapping.put("Low", "low");
mapping.put("Open", "openPrice");
mapping.put("Volume", "volume");

Below is the method that puts it all together.

    public static List<StockQuote> loadAAPL() throws IOException {
        HeaderColumnNameTranslateMappingStrategy<StockQuote> strategy = new HeaderColumnNameTranslateMappingStrategy<StockQuote>();
        strategy.setType(StockQuote.class);

        Hashtable<String,String> mapping = new Hashtable<String,String>();
        mapping.put("Date", "date");
        mapping.put("Close", "closePrice");
        mapping.put("High", "high");
        mapping.put("Low", "low");
        mapping.put("Open", "openPrice");
        mapping.put("Volume", "volume");

        strategy.setColumnMapping(mapping);

        CSVReader csvReader = new CSVReader(new InputStreamReader(TestData.class.getResourceAsStream("stock-aapl.csv")));

        CsvToBean csv = new CsvToBean();

        List<StockQuote> list = csv.parse(strategy, csvReader);

        return list;
    }

I hope you find it as simple and elegant as I do.

No related posts.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>