【问题标题】:Java scanner does not read specific fileJava 扫描器不读取特定文件
【发布时间】:2017-04-20 12:39:50
【问题描述】:

我有一个 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 文件而是接受当前数据的吗?

【问题讨论】:

    标签: java yahoo-finance


    【解决方案1】:

    使用“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
    

    【讨论】:

      【解决方案2】:

      原因是当您调用 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),但从旧 URL 生成的输入流将为空。如果你想模拟你的浏览器做什么,你必须发送一个 HTTP 获取请求,例如像这样:

      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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-20
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多