[英]Java scanner does not read specific file
我有一个Java软件,它要求Yahoo Finance提供当前和历史股价。
如其他文章所述,雅虎可以将价格写入文件,扫描仪可以读取。 要索取亚马逊的当前价格,请致电: http : //finance.yahoo.com/d/quotes.csv? s=AMZ.DE&f= snl1t1c4
要索取过去5年的亚马逊价格,请致电: http : //ichart.finance.yahoo.com/table.csv? s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs
如果我在浏览器中访问这两个链接,它将下载一个.csv文件,其中包含每个文件的预期数据。
在java中,应通过以下方法读取相应的.csv文件:
private static List<String> requestStockData(String request) throws IOException {
Scanner scanner = null;
List<String> answer = new ArrayList();
try {
scanner = new Scanner(new URL(request).openStream());//no exception here
} catch (FileNotFoundException e) {
Tools.printDebugMessage("Received null-answer for request " + request);
return answer;
}
while (scanner.hasNextLine()) {//scanner.hasNextLine() returns false
String value = scanner.nextLine();
answer.add(value);
Tools.printDebugMessage("received answer from YAHOO! Finance: " + value);
}
scanner.close();
return answer;
}
其中request是以上链接之一。
我使用该软件已有几个星期了,并且运行良好。 但是最后几天它不再适用于历史数据,但是对于当前数据仍然可以正常工作。
使用指向历史数据的链接,扫描器将正常打开并且不会引发异常,但是scanner.hasNextLine()将立即返回false,但是随我的浏览器下载的.csv文件有1305行。
你们中有谁知道为什么扫描程序不再接受历史数据的.csv文件,而是接受当前数据的文件?
用“ https”更新您的网址,然后重试。
旧版: http : //ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs
新增: https : //ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs
received answer from YAHOO! Finance: Date,Open,High,Low,Close,Volume,Adj Close
received answer from YAHOO! Finance: 2017-04-19,844.95,849.35,842.90,847.90,1700,847.90
received answer from YAHOO! Finance: 2017-04-18,849.50,851.00,841.25,845.00,3100,845.00
received answer from YAHOO! Finance: 2017-04-17,839.90,839.90,839.90,839.90,000,839.90
原因是当您调用http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs时 ,浏览器将重定向到https://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs (即,您获得了代码301)但输入流从旧网址生成的内容将为空。 如果要模拟浏览器的功能,则必须发送HTTP get请求,例如:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
public class Main {
public static void main(String [] args){
String request = "http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs";
try {
HttpGet httpget = new HttpGet(request);
HttpResponse response = HttpClients.createDefault().execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String filePath = "output.csv";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.