Java Reference
In-Depth Information
Display 10.12
Some Methods in the Class File (part 3 of 3)
public boolean mkdirs()
Makes a directory named by the abstract path name. Will create any necessary but nonexistent
parent 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 designates 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 fi le named Sally.txt .
The program has no input, and the only output tells whether or not there is a
fi le named Sally.txt .
27. Write a complete Java program that asks the user for a fi le name, tests whether
the fi le exists, and, if the fi le exists, asks the user whether or not it should be
deleted. It then either deletes or does not delete the fi le 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
sequence of bytes (same sequence of zeros and ones) whether it is stored in an int
variable in memory or in a binary file. So, no conversion of any kind needs to be
performed when you store or retrieve a value in a binary file. This is why binary files
can be handled more efficiently than text files.
Java binary files are unlike binary files in other programming languages in that
they are portable. A binary file created by a Java program can be moved from one
computer to another and still be read by a Java program—but only by a Java program.
They cannot normally be read with a text editor or with a program written in any
programming language other than Java.
The preferred stream classes for processing binary files are ObjectInputStream
and ObjectOutputStream . Each has methods to read or write data one byte at a time.
These streams can also automatically convert numbers and characters to bytes that can
be stored in a binary file. They allow your program to be written as if the data placed in
 
 
 
Search WWH ::




Custom Search