FileOutputStream Class

Package: java.io

The FileOutputStream class connects an output stream to a File object and provides the basic ability to write binary data to the file.

In most cases, you won’t use methods of this class directly. Instead, you’ll use this class to connect to Buffered OutputStream, which extends the FileOutputStream class by providing buffering for more efficient output. Then, you’ll connect the BufferedOutputStream object to a Data OutputStream object, which has the ability to write primitive data types (such as integers and doubles) directly to the output file. As a result, this section shows only the constructor for the FileOutputStream class and not its methods. For more information, see DataOutputStream Class.

CrossRef.eps The FileOutputStream class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructors

Constructor

Description

FileOutputStream(File file)

Creates a file writer from the file. It throws FileNotFoundException if an error occurs.

FileOutputStream(File file, boolean append)

Creates a file writer from the file. It throws FileNotFoundException if an error occurs. If the second parameter is true, data is added to the end of the file if the file already exists.

FileOutputStream(String path)

Creates a file writer from the specified pathname. It throws FileNotFoundException if an error occurs.

FileOutputStream(String path, boolean append)

Creates a file writer from the specified pathname. It throws FileNotFoundException if an error occurs. If the second parameter is true, data is added to the end of the file if the file already exists.

The following example shows how to create a FileOutputStream object that appends to an existing file:

File f;

FileOutputStream fstream;

f = new File(“myfile.txt”);

fstream = new FileOutputStream(f, true);

And this example shows how you can skip the File object altogether and create the output file directly from a path string:

FileOutputStream fstream

= new FileOutputStream(“myfile.bin”, true);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset