1

说我有一个网址

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

但它每次都抛出异常!

编辑:这是抛出的错误:

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)

predictableprototype 的第 73 行是 } ,而 sig2words 的第 11 行链接到上述方法。

4

1 回答 1

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();
    }
}
于 2014-03-01T15:29:06.710 回答