1

Say I have a url

www.hosting.com/words.bin

how would I read the words hosted in that URL?

I've tried

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

but its throwing an exception everytime!

edit: This is the error beign thrown:

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)

line 73 of predictiveprototype is a } , while line 11 of sig2words links to the above method.

Gwen Wong
  • 357
  • 3
  • 15

1 Answers1

0

This works for me:

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();
    }
}
folkol
  • 4,752
  • 22
  • 25