Java Reference
In-Depth Information
20.7. Working with Files
The java.io package provides a number of classes that help you work
with files in the underlying system. The File stream classes allow you to
read from and write to files and the FileDescriptor class allows the sys-
tem to represent underlying file system resources as objects. RandomAc-
cessFile lets you deal with files as randomly accessed streams of bytes
or characters. Actual interaction with the local file system is through
the File class, which provides an abstraction of file pathnames, includ-
ing path component separators, and useful methods to manipulate file
names.
20.7.1. File Streams and FileDescriptor
The File streams FileInputStream , FileOutputStream , FileReader , and
FileWriter allow you to treat a file as a stream for input or output. Each
type is instantiated with one of three constructors:
A constructor that takes a String that is the name of the file.
A constructor that takes a File object that refers to the file (see
Section 20.7.3 on page 543 ).
A constructor that takes a FileDescriptor object (see below).
If a file does not exist, the input streams will throw a FileNotFoundExcep-
tion . Accessing a file requires a security check and a SecurityException is
thrown if you do not have permission to access that filesee " Security " on
page 677 .
With a byte or character output stream, the first two constructor types
create the file if it does not exist, or truncate it if it does exist. You can
control truncation by using the overloaded forms of these two construct-
ors that take a second argument: a boolean that, if true , causes each in-
dividual write to append to the file. If this boolean is false , the file will be
 
Search WWH ::




Custom Search