23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

Finally, many HTML files contain relative URLs. The fourth constructor in the previous code<br />

creates URLs relative to a given URL and is particularly useful when parsing HTML. For<br />

example, the following code creates a URL pointing to the file 08.html, taking the rest of the<br />

URL from u1:<br />

URL u1, u2;<br />

try {<br />

u1 = new URL("http://metalab.unc.edu/javafaq/course/week12/07.html");<br />

u2 = new URL(u1, "08.html");<br />

}<br />

catch (MalformedURLException e) { }<br />

Once a URL object has been constructed, there are two ways to retrieve its data. The<br />

openStream() method returns a raw stream of bytes from the source. The getContent()<br />

method returns a <strong>Java</strong> object that represents the data. When you call getContent(), <strong>Java</strong><br />

looks for a content handler that matches the MIME type of the data. It is the openStream()<br />

method that is of concern in this book.<br />

The openStream() method makes a socket connection to the server and port specified in the<br />

URL. It returns an input stream from which you can read the data at that URL, allowing you<br />

to download data from the server. Any headers that come before the actual data or file<br />

requested are stripped off before the stream is opened. You get only raw data.<br />

public final InputStream openStream() throws <strong>IO</strong>Exception<br />

You read from the input stream using an input stream or reader class. For example:<br />

try {<br />

URL u = new URL("http://www.amnesty.org/");<br />

InputStream in = u.openStream();<br />

int b;<br />

while ((b = in.read()) != -1) {<br />

System.out.write(b);<br />

}<br />

}<br />

catch (MalformedURLException e) {System.err.println(e);}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

Applets running under the control of a security manager—that is untrusted applets that run<br />

inside a web browser—are normally only allowed to connect back to the host they were<br />

downloaded from. This host can be determined from the URL returned by the<br />

getCodeBase() method of the Applet class. Attempts to connect to other hosts throw<br />

security exceptions. You can create URLs that point to other hosts, but you may not download<br />

data from them using openStream() or any other method. (This security restriction for<br />

applets applies to any network connection, regardless of how you get it.)<br />

Example 5.1 reads a series of URLs from the command line. The program connects to the<br />

specified URLs, downloads the data, and copies it to System.out.<br />

Example 5.1. The URLTyper Program<br />

import java.net.*;<br />

import java.io.*;<br />

import com.macfaq.io.*;<br />

61

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

Saved successfully!

Ooh no, something went wrong!