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.

fin.close();<br />

src.delete();<br />

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

The warnings about copying files (meta-information and resource forks not maintained,<br />

copies file-to-file only supported) go double for this sort of move, because after the move is<br />

complete, the original file and all its data and meta-information are deleted. Any information<br />

that wasn't copied is lost.<br />

12.3.4.5 Changing file attributes<br />

<strong>Java</strong> 2 allows an application to change a file's last modified time:<br />

public boolean setLastModified(long time) // <strong>Java</strong> 2<br />

The time argument is the number of milliseconds since midnight, GMT, January 1, 1970.<br />

This will be converted to the format necessary for a particular platform's file modification<br />

times. If the platform does not support millisecond-accurate file modification times, then the<br />

time will be rounded to the nearest time the host platform does support. An<br />

IllegalArgumentException is thrown if time is negative; a SecurityException is thrown<br />

if the security manager disallows write access to the file.<br />

The setReadOnly() method marks the file so that only reading of the file is allowed. Any<br />

form of writing to the file is disallowed.<br />

public boolean setReadOnly() // <strong>Java</strong> 2<br />

It may be an oversight, but <strong>Java</strong> provides no way to undo the effects of this method and allow<br />

a file to be written. To accomplish that, you'll need to use chmod (Unix), the Properties<br />

window (Windows), the Get Info window (Mac), or whatever facility your platform provides<br />

for locking and unlocking files. A SecurityException is thrown if the security manager<br />

disallows write access to the file.<br />

12.3.5 Temporary Files<br />

<strong>Java</strong> 2 adds support for temporary files: files that only exist as long as the program runs. The<br />

File class provides two methods that create temporary files and one method to mark a file to<br />

be deleted when the program exits.<br />

public static File createTempFile(String prefix, String suffix)<br />

throws <strong>IO</strong>Exception // <strong>Java</strong> 2<br />

public static File createTempFile(String prefix, String suffix,<br />

File directory) throws <strong>IO</strong>Exception // <strong>Java</strong> 2<br />

The createTempFile() methods create a file with a name that begins with the specified<br />

prefix and ends with the specified suffix. The prefix is a string used at the beginning of all<br />

temporary file names; the suffix is appended to the end of all temporary file names. The suffix<br />

may be null. If so, .tmp will be used as the suffix. The same run of the same VM will not<br />

create two files with the same name. For example, consider this for loop:<br />

for (int i=0; i < 10; i++) {<br />

File f1 = File.createTempFile("mail", ".tem");<br />

}<br />

293

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

Saved successfully!

Ooh no, something went wrong!