Read remote .csv file using opencsv

Piccolo picture Piccolo · Mar 20, 2013 · Viewed 8.8k times · Source

I've been pondering this for a fair amount of time now. I'm trying to download the data from Yahoo!'s Stock API. When you use the API, it gives you a .csv file. I've been looking at opencsv, which seems perfect, except I want to avoid downloading and saving the file, if at all possible.

OpenCSV, according to the examples, can only read from a FileReader. According to Oracle's docs on FileReader, the file needs to be local.


Is it possible to read from a remote file using OpenCSV without downloading?

Answer

FThompson picture FThompson · Mar 20, 2013

CSVReader takes a Reader argument according to the documentation, so it isn't limited to a FileReader for the parameter.

To use a CSVReader without saving the file first, you could use a BufferedReader around a stream loading the data:

URL stockURL = new URL("http://example.com/stock.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
CSVReader reader = new CSVReader(in);
// use reader