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.

12.3.3 Listing Information About a File<br />

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

The File class contains many methods that return particular information about the file. Most<br />

of this information can be gleaned from the path field alone without accessing the filesystem.<br />

Therefore, most of these methods do not throw <strong>IO</strong>Exceptions.<br />

12.3.3.1 Does the file exist? Is it a normal file? Is it a directory?<br />

Since a File object does not necessarily correspond to a real file on the disk, the first question<br />

you'll probably want to ask is whether the file corresponding to the File object actually<br />

exists. This is especially important if you're relying on a user to type a filename rather than<br />

select it from a dialog, because users routinely mistype filenames. The exists() method<br />

returns true if the file named in this file object's path field exists, false if it doesn't:<br />

public boolean exists()<br />

There are two other ways to ask this question. The isFile() method returns true if the file<br />

exists and is not a directory. On the other hand, the isDirectory() method returns true if<br />

the file exists and is a directory.<br />

public boolean isFile()<br />

public boolean isDirectory()<br />

The isDirectory() method does consider Unix symbolic links and Mac aliases to directories<br />

to be directories themselves; it does not consider Windows shortcuts to directories to be<br />

directories. All three of these methods throw a security exception if security manager does not<br />

allow the specified file to be read. In fact, if the file couldn't be read if it did exist, then this<br />

exception is thrown whether or not the file actually exists. Even determining whether or not<br />

certain files exist can be considered to be a security violation. Like most security issues, this<br />

is primarily a problem for applets, not applications.<br />

12.3.3.2 Filename and path<br />

The getName() method takes no arguments and returns the name of the file as a string:<br />

public String getName()<br />

The name does not include any part of the directory in which the file lives. That is, you get<br />

back index.html instead of /public/html/javafaq/index.html. If the file is a directory like<br />

/public/html/javafaq/, only the last name is returned (javafaq in this example).<br />

The getPath() method returns the complete path to the file as stored in the File object's path<br />

field:<br />

public String getPath()<br />

This is merely a get method that returns the path field. Therefore, the path is relative if the<br />

File object was constructed with a relative path and absolute if the File object was<br />

constructed with an absolute path. Furthermore, this method never throws <strong>IO</strong>Exceptions.<br />

Consider Example 12.2. This simple program constructs two File objects, one with a relative<br />

path and one with an absolute path, then prints the name and path of each object.<br />

284

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

Saved successfully!

Ooh no, something went wrong!