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.

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

If you need to know whether a file is specified by a relative or absolute path, you can call<br />

isAbsolute():<br />

public boolean isAbsolute()<br />

This does not throw any security exceptions, because it does not need to go outside the class<br />

to determine whether or not a pathname is absolute. Instead, the check is performed by<br />

looking at the first few characters of the path field. On Unix or the Mac an absolute path<br />

begins with a /. On Windows or OS/2 an absolute path begins with a capital letter followed by<br />

a colon and a backslash, like C:\.<br />

12.3.3.4 Canonical paths<br />

Exactly what a canonical path is, and how it differs from an absolute path, is systemdependent,<br />

but it tends to mean that the path is somehow more real than the absolute path.<br />

Typically, if the full path contains aliases, shortcuts, shadows, or symbolic links of some kind,<br />

the canonical path resolves those aliases to the actual directories they refer to. The canonical<br />

path is returned by the getCanonicalPath() method:<br />

public String getCanonicalPath() throws <strong>IO</strong>Exception<br />

For example, on Unix when getAbsolutePath() is invoked on a symbolic link or a file that<br />

has a symbolic link (an alias or shortcut) in its path, the symbolic link is included in the path<br />

that getAbsolutePath() returns. However getCanonicalPath() returns the path with all<br />

symbolic links resolved. For example, suppose /bin/perl is a link to the real file at<br />

/usr/local/bin/perl, and you construct a File object perlLink like this:<br />

File perlLink = new File("/bin/perl");<br />

perlLink.getAbsolutePath() returns /bin/perl, but perlLink.getCanonicalPath()<br />

returns /usr/local/bin/perl.<br />

On Windows and the Macintosh, getAbsolutePath() and getCanonicalPath() both<br />

resolve shortcuts and aliases. Suppose /Macintosh%20HD/Desktop%20Folder/javac is an<br />

alias to /Macintosh%20HD/<strong>Java</strong>/MRJ%202.0%20Early%20Access%<br />

20Rel%202/MRJ%20SDK%202.0/Tools/javac, and a File object named alias is constructed<br />

like this:<br />

File alias = new File("/Macintosh%20HD/Desktop%20Folder/javac");<br />

Both alias.getAbsolutePath() and alias.getCanonicalPath() return<br />

/Macintosh%20HD/<strong>Java</strong>/MRJ%202.0%20Early%20Access%20Rel%202/MRJ%20SDK%202.<br />

0/Tools/javac, not /Macintosh%20HD/Desktop%20Folder/javac . getCanonicalPath()<br />

throws a security exception in most applet environments, because it reveals too much<br />

information about the host the applet is running on. getCanonicalPath() also removes<br />

relative references like the double period (..), which refers to the parent directory in many<br />

command-line environments and in URLs. For instance, suppose the current working<br />

directory is /home/users/elharo/javaio/ioexamples/12. Then you create a File object like this:<br />

File f = new File("../11/index.html");<br />

String absolutePath = f.getAbsolutePath();<br />

286

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

Saved successfully!

Ooh no, something went wrong!