This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Read URL Using


This program, URLdemo, opens a connection to a website specified by a URL (Uniform Resource Locator) to read text from a file at that site. For examples accessing website content using MATLAB® functions, see Web Access.

URLdemo constructs an object of the Java® API class,, which enables convenient handling of URLs. Then, it calls a method on the URL object, to open a connection.

To read and display the lines of text at the site, URLdemo uses classes from the Java I/O package It creates an InputStreamReader object, and then uses that object to construct a BufferedReader object. Finally, it calls a method on the BufferedReader object to read the specified number of lines from the site.

Description of URLdemo

The major tasks performed by URLdemo are:

  1. Construct a URL object.

    The example first calls a constructor on and assigns the resulting object to variable url. The URL constructor takes a single argument, the name of the URL to be accessed. The constructor checks whether the input URL has a valid form.

    url ='')
  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 website 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 constructor is called with the input stream is, to return to variable isr an object that can read characters. Then, the 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 =;
    br =;
  4. Read and display lines of text.

    The following statements read the lines of HTML text from the site, looking for text beginning with a paragraph tag, <p>. Within the MATLAB while loop, the BufferedReader method readLine reads each line of text (terminated by a return and/or line feed character) from the site.

    p1 = java.lang.String('<p>');
    p2 = java.lang.String('</p>');
    s = readLine(br);
    while ~(s.startsWith(p1))
       s = readLine(br);
  5. Display the text.


Running the Example

When you run this example, you see output similar to the following.

Explore products for MATLAB, the language of technical computing, and Simulink, for simulation and Model-Based Design.

More About

Was this topic helpful?