O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required