Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Read URL Using java.net.URL

This example shows how to display HTML text from the first paragraph on the www.mathworks.com website.

Construct URL Object

Call a java.net.URL constructor to create object url pointing to the MathWorks website.

url = java.net.URL('http://www.mathworks.com');

Open Connection to URL

Call the openStream method to establish a connection with the website. The method creates an InputStream object is for reading bytes from the site.

is = openStream(url);

Set Up Buffered Stream Reader

Create a buffered stream reader isr for reading characters using the java.io.InputStreamReader constructor. Create a buffered reader object br for efficient reading of characters, arrays, and lines.

isr = java.io.InputStreamReader(is);
br = java.io.BufferedReader(isr);

Read Lines of Text

Read lines of HTML text from the website which are within the first paragraph tag '<p>'. The BufferedReader method readLine reads a line of text which is terminated by a carriage return and/or line feed character.

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

Display Text

disp(s.substring(p1.length,s.length-p2.length))
<p>Explore products for <a href="/products/matlab/?s_tid=hp_fp_ml">MATLAB</a>, the language of technical computing, and <a href="/products/simulink/?s_tid=hp_fp_sl">Simulink</a>, for simulation and Model-Based Design.</p>

Was this topic helpful?