Java扫描程序不读取特定文件
我有一个java软件,它可以向雅虎财经索取当前和历史的股票价格 如其他帖子所述,雅虎可以将价格写入一个文件,该文件可以被扫描仪读取。 要查询亚马逊的当前价格,请致电: 要索取亚马逊过去5年的价格,我打电话: 如果我在浏览器中访问这两个链接,它将下载一个.csv文件,其中包含每个链接的预期数据 在java中,应通过folling方法读取相应的.csv文件:Java扫描程序不读取特定文件,java,yahoo-finance,Java,Yahoo Finance,我有一个java软件,它可以向雅虎财经索取当前和历史的股票价格 如其他帖子所述,雅虎可以将价格写入一个文件,该文件可以被扫描仪读取。 要查询亚马逊的当前价格,请致电: 要索取亚马逊过去5年的价格,我打电话: 如果我在浏览器中访问这两个链接,它将下载一个.csv文件,其中包含每个链接的预期数据 在java中,应通过folling方法读取相应的.csv文件: private static List<String> requestStockData(String request) th
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;
}
私有静态列表请求StockData(字符串请求)引发IOException{
扫描器=空;
列表答案=新建ArrayList();
试一试{
scanner=new scanner(新URL(request).openStream());//这里没有异常
}catch(filenotfounde异常){
printDebugMessage(“收到请求的空应答”+请求);
返回答案;
}
而(scanner.hasnetline()){//scanner.hasnetline()返回false
字符串值=scanner.nextLine();
答:增加(价值);
printDebugMessage(“收到雅虎财经的回复:“+value”);
}
scanner.close();
返回答案;
}
其中请求是上面的链接之一
我使用这个软件有几个星期了,它工作得非常好。
但在过去的日子里,它不再适用于历史数据,但适用于当前数据
使用指向历史数据的链接,扫描仪将正常打开,不会引发异常,但scanner.hasNextLine()会立即返回false,但使用我的浏览器下载的.csv文件有1305行
有人知道为什么扫描仪不再接受历史数据的.csv文件,而是接受当前数据吗?用“https”更新您的url,然后重试
旧的:
新的:
请使用“https”更新您的url,然后重试
旧的:
新的:
原因是,当您调用浏览器时,浏览器被重定向到(即返回代码301),但从旧URL生成的输入流将为空。如果要模拟浏览器的功能,必须发送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();
}
}
}
原因是,当您调用浏览器时,浏览器被重定向到(即返回代码301),但从旧URL生成的输入流将为空。如果要模拟浏览器的功能,必须发送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();
}
}
}