Read remote .csv file using opencsv

橙三吉。 提交于 2019-12-23 07:36:47

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/15513697/read-remote-csv-file-using-opencsv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!