Up to now we have been content to process input located on the local computer. In this lesson,
we will read a file from the Internet using the class java.net.URL and its "openStream" function.
Here is the example:
The above example reads the contents of a html file on the Internet. Try typing in
"http://bimopensource.freewebspace.com/CenterFrame.html" in the text field with the label "URL:"
then pressing the "Open Stream" button. The contents of the file are displayed in the
textarea called "txtArea".
In the "actionPerformed" function of OpenStream the URL is obtained by getting the text
from txtURL and checking to make sure that the user actually typed in the URL. Once,
a URL is typed in and the button "Open Stream" is clicked the "openStream" function,
which returns a java.io.InputStream, is called and passed into a InputStreamReader which
is then passed into a BufferedReader. Each line in the file is read by using the
BufferedReader's "readLine" function until a null String is returned.
If a String that isn't compatible with the URL object's constructor is passed to the
constructor then an Exception is thrown and the stack trace is printed out to the
textarea "txtArea".
In this lesson, you learned how to use "openStream" of the URL class to read in
a html file from the Internet and display its contents in a TextArea.