我正在尝试将 cdv 文件读入我在 mac 上用 eclipse 编写的 java 程序。当我从计算机上的目录中读取文件时,这很好用,但我想从网络上读取 csv 文件(在我的例子中是雅虎财经)。这在 PC 上工作得非常好,所以它可能与 Mac 相关。我已经测试了几个不同的选项,使用反斜杠、多个反斜杠等,但我无法让它工作,所以我求助于你们这里的聪明人。
这是我的代码:
public void run() {
String csvFile = "http://ichart.finance.yahoo.com/table.csv?s=SPHS&a=05&b=9&c=2013&d=09&e=12&f=2013&g=d&ignore=.csv";
System.out.println(csvFile);
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] output = line.split(csvSplitBy);
System.out.println("On "+output[0]+" Closing price was: "+output[6]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done!");
}
这是我得到的错误和输出:
http:\\ichart.finance.yahoo.com\table.csv?s=SPHS&a=05&b=9&c=2013&d=09&e=12&f=2013&g=d&ignore=.csv
java.io.FileNotFoundException: http:\\ichart.finance.yahoo.com\table.csv?s=SPHS&a=05&b=9&c=2013&d=09&e=12&f=2013&g=d&ignore=.csv (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at ReadCSV.ReadCSV.run(ReadCSV.java:28)
at ReadCSV.ReadCSV.main(ReadCSV.java:13)
Done!