Piccolo Piccolo - 1 year ago 109
Java Question

Read remote .csv file using opencsv

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

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
. According to Oracle's docs on
, the file needs to be local.

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download