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 />

The isHidden() method, only available in <strong>Java</strong> 2 and later, returns true if the file exists but<br />

is hidden; that is, it does not appear in normal displays or listings. It returns false if the file<br />

isn't hidden or doesn't exist.<br />

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

Exactly how a file is hidden varies from platform to platform. On Unix, any file whose name<br />

begins with a period is hidden. On Windows and the Mac, hidden files are identified by<br />

particular attributes. This method throws a security exception if the security manager doesn't<br />

allow the file to be read.<br />

The lastModified() method returns a long indicating the last time this file was modified:<br />

public long lastModified()<br />

<strong>Java</strong> 2 specifies that the long returned is the number of milliseconds since midnight, January<br />

1, 1970, Greenwich Mean Time. However, in earlier VMs the conversion between this long<br />

and a real date is platform-dependent, so it's only useful for comparing the modification dates<br />

of different files, not for determining the absolute time a file was modified. This method<br />

throws a security exception if the security manager doesn't allow the file to be read. It returns<br />

if the file doesn't exist or the last modified date can't be determined.<br />

Finally, the length() method returns the number of bytes in the file or if the file does not<br />

exist:<br />

public long length()<br />

This method throws a security exception if the security manager doesn't allow the file to be<br />

read.<br />

12.3.3.7 An example<br />

Example 12.4 is a character-mode program that lists all the available information about files<br />

named on the command line. Names may be given as absolute or relative paths.<br />

Example 12.4. The FileSpy Program<br />

import java.io.*;<br />

import java.util.*;<br />

public class FileSpy {<br />

public static void main(String[] args) {<br />

for (int i = 0; i < args.length; i++) {<br />

File f = new File(args[i]);<br />

if (f.exists()) {<br />

System.out.println("Name: " + f.getName());<br />

System.out.println("Absolute path: " + f.getAbsolutePath());<br />

try {<br />

System.out.println("Canonical path: " + f.getCanonicalPath());<br />

}<br />

289

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

Saved successfully!

Ooh no, something went wrong!