External Interfaces/API | ![]() ![]() |
The major tasks performed by URLdemo
are:
1. Construct a URL Object
The example first calls a constructor on java.net.URL
and assigns the resulting object to variable url
. The URL
constructor takes a single argument, the name of the URL to be accessed, as a string. The constructor checks whether the input URL has a valid form.
url = java.net.URL(... 'http://www.ncsa.uiuc.edu/demoweb/url-primer.html')
2. Open a Connection to the URL
The second statement of the example calls the method, openStream
, on the URL
object url
, to establish a connection with the web site named by the object. The method returns an InputStream
object to variable, is
, for reading bytes from the site.
is = openStream(url)
3. Set Up a Buffered Stream Reader
The next two lines create a buffered stream reader for characters. The java.io.InputStreamReader
constructor is called with the input stream is
, to return to variable isr
an object that can read characters. Then, the java.io.BufferedReader
constructor is called with isr
, to return a BufferedReader
object to variable br
. A buffered reader provides for efficient reading of characters, arrays, and lines.
isr = java.io.InputStreamReader(is) br = java.io.BufferedReader(isr)
4. Read and Display Lines of Text
The final statement reads and displays the first 10 lines of text from the site. Within a MATLAB for
statement that iterates 10 times, the BufferedReader
method readLine
reads a line of text (terminated by a return
and/or line feed character) from the site. To display the output in MATLAB, assign it to variable s
, without terminating the statement with a semicolon.
for i = 1:10 s = readLine(br) end
![]() | Example - Reading a URL | Running the Example | ![]() |