Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Downloading CSV Files with Java

Tags:

java

http

Scenario: A website I use to research stock data has a link on the page to Export Data to Spreadsheet. The URL displayed when hovering over the export link is of the form http://www.stocksite.com/historical/export.php?symbol=C .

Question: Rather, that manually visiting the page for each stock I would like to automate the task. From Java, how can I programmatically call the site with a stock symbol and save the exported csv file? The URL and URLConnection class seem like the obvious place to start, but I'm unsure where to go from there.

like image 869
javacavaj Avatar asked Dec 10 '22 12:12

javacavaj


1 Answers

All you need to do is to get the CSV in flavor of an InputStream.

InputStream input = new URL("http://example.com/file.csv").openStream();

Then you can feed it to any decent Java CSV parser API. Almost any of them take input in flavor of InputStream or Reader. If necessary, you can easily decorate InputStream as a Reader using InputStreamReader which you can then feed to the CSV parser.

Reader reader = new InputStreamReader(input, "UTF-8");
like image 97
BalusC Avatar answered Dec 13 '22 23:12

BalusC