|
|
|
|||
|
How to Use File StreamsFile streams are perhaps the easiest streams to understand. Simply put, the file streams--FileReaderFileWriterFileInputStreamFileOutputStreamFileFileDescriptor
The following
This program is very simple.
It opens a FileReader on farrago.txt and
opens a FileWriter on outagain.txt.
The program reads characters from
the reader as long as there's more input in the input file.
When the input runs out,
the program closes both the reader and the writer.
Notice the code that the
This code creates a File object that represents the named
file on the native file system. File is a utility class
provided by java.io. This program uses this object only to
construct a FileReader on farrago.txt.
However, it could use inputFile to get information about
farrago.txt, such as its full pathname.
After you've run the program, you should find an exact copy of
Remember thatSo she went into the garden to cut a cabbage-leaf, to make an apple-pie; and at the same time a great she-bear, coming up the street, pops its head into the shop. 'What! no soap?' So he died, and she very imprudently married the barber; and there were present the Picninnies, and the Joblillies, and the Garyalies, and the grand Panjandrum himself, with the little round button at top, and they all fell to playing the game of catch as catch can, till the gun powder ran out at the heels of their boots. Samuel Foote 1720-1777 FileReader and FileWriter read
and write 16-bit characters. However, most native file systems are
based on 8-bit bytes. These streams encode the characters as they
operate according to the default character-encoding scheme. You can
find out the default character-encoding by using
System.getProperty("file.encoding"). To specify an
encoding other than the default, you should construct an
OutputStreamWriter on a FileOutputStream and
specify it. For information about encoding characters, see the
Internationalization
For the curious, here is another version of this program,
CopyBytes,
which uses | |||
|