可以BufferedReader读取在线托管的文本文件吗?

时间:2014-03-01 15:20:05

标签: java bufferedreader inputstreamreader

说我有个网址

www.hosting.com/words.bin

我如何阅读该网址中托管的字词?

我试过

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

但它每次都抛出异常!

编辑:这是抛出的错误:

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)

预测原型的第73行是} 虽然sig2words的第11行链接到上述方法。

1 个答案:

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