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.

public int hashCode()<br />

public boolean equals(Object o)<br />

public String toString()<br />

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

In <strong>Java</strong> 2, the File class implements the java.lang.Comparable interface so that two<br />

pathnames may be compared to each other. This requires these two compareTo() methods:<br />

public int compareTo(File pathname) // <strong>Java</strong> 2<br />

public int compareTo(Object o) // <strong>Java</strong> 2<br />

For example:<br />

File f1 = new File("readme.txt");<br />

File f2 = new File("README.TXT");<br />

int result = f1.compareTo(f2);<br />

This method returns a number less than zero if f1 is less than f2, zero if they're the same, and<br />

a positive number if f1 is greater than f2. The comparison is made against the path fields of<br />

f1 and f2. The algorithm used for comparison is more or less alphabetical. On caseinsensitive<br />

platforms like Windows and the Mac, case is not considered. For instance, the<br />

previous result would be zero on the Mac or Windows but positive on Unix.<br />

12.3.7 Working with Directories<br />

A File object can represent a directory as easily as a file. Most of the File methods, like<br />

getName(), canWrite(), and getPath(), behave exactly the same for a directory as they do<br />

for a file. However, there are a couple of methods in the File class that behave differently<br />

when they operate on directories than they do when operating on ordinary files.<br />

The delete() method only works on empty directories. If a directory contains even one file,<br />

it can't be easily deleted. If you attempt to delete a nonempty directory, delete() fails and<br />

returns false. No exception is thrown.<br />

The renameTo() method works on both empty and nonempty directories. However—whether<br />

a directory is empty or not—renameTo() can only rename it, not move it to a different<br />

directory. If you attempt to move a directory into another directory, renameTo() fails and<br />

returns false. No exception is thrown.<br />

The File class also has several methods that just work with directories, not with regular files.<br />

12.3.7.1 Creating directories<br />

To create a file, you open a FileOutputStream to it (<strong>Java</strong> 1.0 and 1.1) or call<br />

createNewFile() (<strong>Java</strong> 2). This doesn't work for directories, though. For that purpose, the<br />

File class has a mkdir() method:<br />

public boolean mkdir()<br />

The mkdir() method attempts to create a directory with the path specified in the path field. If<br />

the directory is created, the method returns true. For example:<br />

295

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

Saved successfully!

Ooh no, something went wrong!