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

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

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

Chapter 5. Network Streams<br />

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

From its first days, <strong>Java</strong> has had the network in mind, more so than any other common<br />

programming language. <strong>Java</strong> is the first programming language to provide as much support<br />

for network I/O as it does for file I/O, perhaps even more—<strong>Java</strong>'s URL, URLConnection,<br />

Socket, and ServerSocket classes are all fertile sources of streams. The exact type of the<br />

stream used by a network connection is typically hidden inside the undocumented sun classes.<br />

Thus, network I/O relies primarily on the basic InputStream and OutputStream methods,<br />

which you can wrap with any higher-level stream that suits your needs: buffering,<br />

cryptography, compression, or whatever your application requires.<br />

5.1 URLs<br />

The java.net.URL class represents a Uniform Resource Locator like<br />

http://metalab.unc.edu/javafaq/. Each URL unambiguously identifies the location of a<br />

resource on the Internet. The URL class has four constructors. All are declared to throw<br />

MalformedURLException, a subclass of <strong>IO</strong>Exception.<br />

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

public URL(String protocol, String host, String file)<br />

throws MalformedURLException<br />

public URL(String protocol, String host, int port, String file)<br />

throws MalformedURLException<br />

public URL(URL context, String u) throws MalformedURLException<br />

A MalformedURLException is thrown if the constructor's arguments do not specify a valid<br />

URL. Often this means a particular <strong>Java</strong> implementation does not have the right protocol<br />

handler installed. Thus, given a complete absolute URL like<br />

http://www.poly.edu/schedule/fall97/bgrad.html#cs, you construct a URL object like this:<br />

URL u = null;<br />

try {<br />

u = new URL("http://www.poly.edu/schedule/fall97/bgrad.html#cs");<br />

}<br />

catch (MalformedURLException e) { }<br />

You can also construct the URL object by passing its pieces to the constructor:<br />

URL u = null;<br />

try {<br />

u = new URL("http", "www.poly.edu", "/schedule/fall97/bgrad.html#cs");<br />

}<br />

catch (MalformedURLException e) { }<br />

You don't normally need to specify a port for a URL; most protocols have default ports. For<br />

instance, the HTTP port is 80. Sometimes the port used does change, and in that case you can<br />

use the third constructor:<br />

URL u = null;<br />

try {<br />

u = new URL("http", "www.poly.edu", 80,"/schedule/fall97/bgrad.html#cs");<br />

}<br />

catch (MalformedURLException e) { }<br />

60

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

Saved successfully!

Ooh no, something went wrong!