26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

962 Files and Streams Chapter 16<br />

of that file without warning. If a program using a File determines that a file already exists,<br />

the program can warn that the user is about <strong>to</strong> discard the original file’s contents.<br />

Good <strong>Program</strong>ming Practice 16.2<br />

Use a File object <strong>to</strong> determine whether a file exists before opening the file with a File-<br />

OutputStream object. 16.2<br />

Class File provides three construc<strong>to</strong>rs. The construc<strong>to</strong>r<br />

public File( String name )<br />

s<strong>to</strong>res the String argument name in the object. The name can contain path information<br />

as well as a file or direc<strong>to</strong>ry name. A file or direc<strong>to</strong>ry’s path specifies the location of the file<br />

or direc<strong>to</strong>ry on disk. The path includes some or all of the direc<strong>to</strong>ries leading <strong>to</strong> the file or<br />

direc<strong>to</strong>ry. An absolute path contains all the direc<strong>to</strong>ries, starting with the root direc<strong>to</strong>ry that<br />

lead <strong>to</strong> a specific file or direc<strong>to</strong>ry. Every file or direc<strong>to</strong>ry on a particular disk drive has the<br />

same root direc<strong>to</strong>ry in its path. A relative path contains a subset of the direc<strong>to</strong>ries leading<br />

<strong>to</strong> a specific file or direc<strong>to</strong>ry. Relative paths normally start from the direc<strong>to</strong>ry in which the<br />

application began executing.<br />

The construc<strong>to</strong>r<br />

public File( String pathToName, String name )<br />

uses argument pathToName (an absolute or relative path) <strong>to</strong> locate the file or direc<strong>to</strong>ry<br />

specified by name.<br />

The construc<strong>to</strong>r<br />

public File( File direc<strong>to</strong>ry, String name )<br />

uses an existing File object direc<strong>to</strong>ry (an absolute or relative path) <strong>to</strong> locate the file<br />

or direc<strong>to</strong>ry specified by name.<br />

Figure 16.21 discusses some common File methods. See the <strong>Java</strong> API for other<br />

File methods.<br />

Method Description<br />

boolean canRead() Returns true if a file is readable; false otherwise.<br />

boolean canWrite() Returns true if a file is writable; false otherwise.<br />

boolean exists() Returns true if the name specified as the argument <strong>to</strong> the<br />

File construc<strong>to</strong>r is a file or direc<strong>to</strong>ry in the specified path;<br />

false otherwise.<br />

boolean isFile() Returns true if the name specified as the argument <strong>to</strong> the<br />

File construc<strong>to</strong>r is a file; false otherwise.<br />

boolean isDirec<strong>to</strong>ry() Returns true if the name specified as the argument <strong>to</strong> the<br />

File construc<strong>to</strong>r is a direc<strong>to</strong>ry; false otherwise.<br />

boolean isAbsolute() Returns true if the arguments specified <strong>to</strong> the File construc<strong>to</strong>r<br />

indicate an absolute path <strong>to</strong> a file or direc<strong>to</strong>ry;<br />

false otherwise.<br />

Fig. Fig. 16.21 16.21 Some commonly used File methods (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!