19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

542 Chapter 14 Exception Handling and Text I/O<br />

java.io.File<br />

+File(pathname: String)<br />

+File(parent: String, child: String)<br />

+File(parent: File, child: String)<br />

+exists(): boolean<br />

+canRead(): boolean<br />

+canWrite(): boolean<br />

+isDirec<strong>to</strong>ry(): boolean<br />

+isFile(): boolean<br />

+isAbsolute(): boolean<br />

+isHidden(): boolean<br />

+getAbsolutePath(): String<br />

+getCanonicalPath(): String<br />

+getName(): String<br />

+getPath(): String<br />

+getParent(): String<br />

+lastModified(): long<br />

+length(): long<br />

+listFile(): File[]<br />

+delete(): boolean<br />

+renameTo(dest: File): boolean<br />

+mkdir(): boolean<br />

+mkdirs(): boolean<br />

Creates a File object for the specified path name. The path name may be a<br />

direc<strong>to</strong>ry or a file.<br />

Creates a File object for the child under the direc<strong>to</strong>ry parent. The child may be<br />

a file name or a subdirec<strong>to</strong>ry.<br />

Creates a File object for the child under the direc<strong>to</strong>ry parent. The parent is a<br />

File object. In the preceding construc<strong>to</strong>r, the parent is a string.<br />

Returns true if the file or the direc<strong>to</strong>ry represented by the File object exists.<br />

Returns true if the file represented by the File object exists and can be read.<br />

Returns true if the file represented by the File object exists and can be written.<br />

Returns true if the File object represents a direc<strong>to</strong>ry.<br />

Returns true if the File object represents a file.<br />

Returns true if the File object is created using an absolute path name.<br />

Returns true if the file represented in the File object is hidden. The exact<br />

definition of hidden is system-dependent. On Windows, you can mark a file<br />

hidden in the File Properties dialog box. On Unix systems, a file is hidden if<br />

its name begins with a period(.) character.<br />

Returns the <strong>com</strong>plete absolute file or direc<strong>to</strong>ry name represented by the File<br />

object.<br />

Returns the same as getAbsolutePath() except that it removes redundant<br />

names, such as "." and "..", from the path name, resolves symbolic links (on<br />

Unix), and converts drive letters <strong>to</strong> standard uppercase (on Windows).<br />

Returns the last name of the <strong>com</strong>plete direc<strong>to</strong>ry and file name represented by<br />

the File object. For example, new File("c:\\book\\test.dat").getName() returns<br />

test.dat.<br />

Returns the <strong>com</strong>plete direc<strong>to</strong>ry and file name represented by the File object.<br />

For example, new File("c:\\book\\test.dat").getPath() returns c:\book\test.dat.<br />

Returns the <strong>com</strong>plete parent direc<strong>to</strong>ry of the current direc<strong>to</strong>ry or the file<br />

represented by the File object. For example, new<br />

File("c:\\book\\test.dat").getParent() returns c:\book.<br />

Returns the time that the file was last modified.<br />

Returns the size of the file, or 0 if it does not exist or if it is a direc<strong>to</strong>ry.<br />

Returns the files under the direc<strong>to</strong>ry for a direc<strong>to</strong>ry File object.<br />

Deletes the file or direc<strong>to</strong>ry represented by this File object.The method returns<br />

true if the deletion succeeds.<br />

Renames the file or direc<strong>to</strong>ry represented by this File object <strong>to</strong> the specified name<br />

represented in dest. The method returns true if the operation succeeds.<br />

Creates a direc<strong>to</strong>ry represented in this File object. Returns true if the the direc<strong>to</strong>ry is<br />

created successfully.<br />

Same as mkdir() except that it creates direc<strong>to</strong>ry along with its parent direc<strong>to</strong>ries if<br />

the parent direc<strong>to</strong>ries do not exist.<br />

FIGURE 14.6 The File class can be used <strong>to</strong> obtain file and direc<strong>to</strong>ry properties, <strong>to</strong> delete and rename files and direc<strong>to</strong>ries,<br />

and <strong>to</strong> create direc<strong>to</strong>ries.<br />

\ in file names<br />

Caution<br />

The direc<strong>to</strong>ry separa<strong>to</strong>r for Windows is a backslash (\). The backslash is a special character<br />

in <strong>Java</strong> and should be written as \\ in a string literal (see Table 2.6).<br />

Note<br />

Constructing a File instance does not create a file on the machine. You can create a<br />

File instance for any file name regardless whether it exists or not. You can invoke the<br />

exists() method on a File instance <strong>to</strong> check whether the file exists.<br />

relative file name<br />

<strong>Java</strong> direc<strong>to</strong>ry separa<strong>to</strong>r (/)<br />

Do not use absolute file names in your program. If you use a file name such as<br />

c:\\book\\Wel<strong>com</strong>e.java, it will work on Windows but not on other platforms. You<br />

should use a file name relative <strong>to</strong> the current direc<strong>to</strong>ry. For example, you may create a File<br />

object using new File("Wel<strong>com</strong>e.java") for the file Wel<strong>com</strong>e.java in the current direc<strong>to</strong>ry.<br />

You may create a File object using new File("image/us.gif") for the file us.gif<br />

under the image direc<strong>to</strong>ry in the current direc<strong>to</strong>ry. The forward slash (/) is the <strong>Java</strong> direc<strong>to</strong>ry

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

Saved successfully!

Ooh no, something went wrong!