Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read remote .csv file using opencsv

Tags:

java

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 .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?

like image 879
Piccolo Avatar asked Mar 20 '13 01:03

Piccolo


People also ask

How do you read and write CSV file in Java using OpenCSV?

You can Download OpenCSV Jar and include in your project class path. CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file.

Can we read CSV file using Apache POI?

Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import. This is assuming that your CSV has the . csv instead of the .

How do I read a CSV file in Java by line?

We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.


1 Answers

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
like image 65
FThompson Avatar answered Sep 28 '22 04:09

FThompson