9.2. Creating File Output Streams

You use a FileOutputStream object when you want to write to a physical file on a disk. The FileOutputStream class is derived from the OutputStream class and therefore inherits the methods of that class for writing to a file. However, I won't bother going into detail on these or the versions in the FileOutputStream class that override them, as you will be using the new file channel capability to write to a file.

There are five constructors for FileOutputStream objects:

ConstructorDescription
FileOutputStream(
   String filename)
Creates an output stream for the file filename. The existing contents of the file will be overwritten. If the file cannot be opened for any reason, an exception of type FileNotFoundException will be thrown.
FileOutputStream(
    String filename,
    boolean append)
Creates an output stream for the file filename. Data written to the file will be appended following the existing contents if append is true. If append is false, any existing file contents will be overwritten. If the file cannot be opened for any reason, an exception of type FileNotFoundException will be thrown.
FileOutputStream(
    File file)
Creates a file output stream for the file represented by the object file. Any existing file contents will be overwritten. If the file cannot be opened, an exception of type FileNotFoundException will be thrown.
FileOutputStream(
    File file,
    boolean append)
Creates a file output stream for the file represented by the object file. Data written ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.