0
我有這種方法,從雅虎財務下載.csv文件並將它們保存在本地。它在一個循環中被訪問,所以它從列表中下載了許多文件。但是有時候符號輸入不正確,不再存在,或連接超時。如何修改此方法,以便連接超時被重試,並且不正確的符號(意味着URL不起作用)只是跳過而不結束程序?錯誤處理URLConnection
public static void get_file(String symbol){
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
String destination = "C:/"+symbol+"_table.csv";
try {
URL Url;
byte[] buf;
int ByteRead,ByteWritten=0;
Url= new URL(finance_url);
outStream = new BufferedOutputStream(new FileOutputStream(destination));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
}catch (Exception e) {
System.out.println("Error while downloading "+symbol);
e.printStackTrace();
}finally {
try {
is.close();
outStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
你的exeption將被try/catch塊捕獲。問題是什麼?如果出現這樣的問題,你的程序是做什麼的,你希望它做什麼? – 2012-01-16 15:58:50