19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.13 Reading Data from the Web 551<br />

14.13 Reading Data from the Web<br />

Just like you can read data from a file on your <strong>com</strong>puter, you can read data from a file<br />

on the Web.<br />

In addition <strong>to</strong> reading data from a local file on a <strong>com</strong>puter or file server, you can also access<br />

data from a file that is on the Web if you know the file’s URL (Uniform Resource Loca<strong>to</strong>r—<br />

the unique address for a file on the Web). For example, www.google.<strong>com</strong>/index.html is the URL<br />

for the file index.html located on the Google Web server. When you enter the URL in a Web<br />

browser, the Web server sends the data <strong>to</strong> your browser, which renders the data graphically.<br />

Figure 14.11 illustrates how this process works.<br />

Key<br />

Point<br />

Client<br />

Web<br />

Browser<br />

Application<br />

Program<br />

Internet<br />

Server<br />

Web<br />

Server<br />

Local files<br />

FIGURE 14.11<br />

The client retrieves files from a Web server.<br />

For an application program <strong>to</strong> read data from a URL, you first need <strong>to</strong> create a URL object<br />

using the java.net.URL class with this construc<strong>to</strong>r:<br />

public URL(String spec) throws MalformedURLException<br />

For example, the following statement creates a URL object for http://www.google<br />

.<strong>com</strong>/index.html.<br />

1 try {<br />

2 URL url = new URL("http://www.google.<strong>com</strong>/index.html");<br />

3 }<br />

4 catch (MalformedURLException ex) {<br />

5 ex.printStackTrace();<br />

6 }<br />

A MalformedURLException is thrown if the URL string has a syntax error. For example,<br />

the URL string “http:/<br />

www.google.<strong>com</strong>/index.html” would cause a MalformedURLException<br />

runtime error because two slashes (//) are required after the colon (:). Note that the<br />

http:// prefix is required for the URL class <strong>to</strong> recognize a valid URL. It would be wrong if<br />

you replace line 2 with the following code:<br />

URL url = new URL("www.google.<strong>com</strong>/index.html");<br />

After a URL object is created, you can use the openStream() method defined in the URL<br />

class <strong>to</strong> open an input stream and use this stream <strong>to</strong> create a Scanner object as follows:<br />

Scanner input = new Scanner(url.openStream());<br />

Now you can read the data from the input stream just like from a local file. The example in<br />

Listing 14.17 prompts the user <strong>to</strong> enter a URL and displays the size of the file.<br />

LISTING 14.17<br />

1 import java.util.Scanner;<br />

2<br />

ReadFileFromURL.java

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!