2014-03-01 64 views
0

說我有一個網址可以通過BufferedReader在線讀取文本文件嗎?

www.hosting.com/words.bin

我將如何在URL中讀取託管的話?

我已經試過

try { 
    URL url = new URL(FILE_NAME); 
    reader = new BufferedReader(new InputStreamReader(url.openStream())); 
    String word; 
    while ((word = reader.readLine()) != null) 
     //do code 
    reader.close(); 

} catch (IOException e) { e.printStackTrace(); } 

但其每次拋出一個異常!

編輯:這是錯誤beign拋出:

java.io.FileNotFoundException: http://www.hosting.com/words.bin 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at java.net.URL.openStream(Unknown Source) 
    at predictive.PredictivePrototype.signatureToWords(PredictivePrototype.java:73) 
    at predictive.Sigs2WordsProto.main(Sigs2WordsProto.java:11) 

線predictiveprototype 73是一個} ,而sig2words鏈接於上述方法線11。

+3

和異常的堆棧跟蹤?你不認爲錯誤信息可以幫助識別問題嗎? –

+0

您是否將協議http://添加到FILE_NAME的開頭? – kshikama

+0

什麼樣的例外?你能發佈整個堆棧跟蹤嗎? –

回答

0

這個工作對我來說:

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 

public class BufferedReaderExample { 
    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://www.stackoverflow.com"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
     String word; 
     while ((word = reader.readLine()) != null) 
      System.out.println(word); 
     reader.close(); 
    } 
} 
相關問題