Java Reference
In-Depth Information
Display 10.12 Some Methods in the Class File (part 2 of 2)
public boolean isFile()
Returns true if a file exists that is named by the abstract path name and the file is a normal file; oth-
erwise returns false . The meaning of normal is system dependent. Any file created by a Java pro-
gram is guaranteed to be normal.
public boolean isDirectory()
Returns true if a directory (folder) exists that is named by the abstract path name; otherwise returns
false .
public boolean mkdir()
Makes a directory named by the abstract path name. Will not create parent directories. See mkdirs .
Returns true if successful; otherwise returns false .
public boolean mkdirs()
Makes a directory named by the abstract path name. Will create any necessary but nonexistent par-
ent directories. Returns true if successful; otherwise returns false . Note that if it fails, then some
of the parent directories may have been created.
public long length()
Returns the length in bytes of the file named by the abstract path name. If the file does not exist or the
abstract path name names a directory, then the value returned is not specified and may be anything.
Self-Test Exercises
26. Write a complete (although simple) Java program that tests whether or not the
directory (folder) containing the program also contains a file named Sally.txt .
The program has no input and the only output tells whether or not there is a file
named Sally.txt .
27. Write a complete Java program that asks the user for a file name, tests whether
the file exists, and, if the file exists, asks the user whether or not it should be
deleted. It then either deletes or does not delete the file as the user requests.
10.4
Binary Files
A little more than kin, and less than kind.
WILLIAM SHAKESPEARE, Hamlet
Binary files store data in the same format that is used in the computer's memory to
store the values of variables. For example, a value of type int is stored as the same
 
Search WWH ::




Custom Search