Java Reference
In-Depth Information
To open a file for output, create a FileOutputStream object. Here are two commonly used
constructors:
FileOutputStream(String fileName ) throws FileNotFoundException
FileOutputStream(String fileName , boolean append )
throws FileNotFoundException
If the file cannot be created, then FileNotFoundException is thrown. In the first form,
when an output file is opened, any preexisting file by the same name is destroyed. In the
second form, if append is true , then output is appended to the end of the file. Otherwise,
the file is overwritten.
To write to a file, you will use the write( ) method. Its simplest form is shown here:
void write(int byteval ) throws IOException
This method writes the byte specified by byteval to the file. Although byteval is declared as
an integer, only the low-order 8 bits are written to the file. If an error occurs during writing,
an IOException is thrown.
Once you are done with an output file, you must close it using close( ) , shown here:
void close( ) throws IOException
Closing a file releases the system resources allocated to the file, allowing them to be used
by another file. It also ensures that any output remaining in an output buffer is actually
written to the physical device.
The following example copies a text file. The names of the source and destination files
are specified on the command line.
Search WWH ::




Custom Search