public class

FileOutputStream

extends OutputStream
implements Closeable
java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.FileOutputStream
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

A specialized OutputStream that writes to a file in the file system. All write requests made by calling methods in this class are directly forwarded to the equivalent function of the underlying operating system. Since this may induce some performance penalty, in particular if many small write requests are made, a FileOutputStream is often wrapped by a BufferedOutputStream.

Summary

Public Constructors
FileOutputStream(File file)
Constructs a new FileOutputStream on the File file.
FileOutputStream(File file, boolean append)
Constructs a new FileOutputStream on the File file.
FileOutputStream(FileDescriptor fd)
Constructs a new FileOutputStream on the FileDescriptor fd.
FileOutputStream(String filename)
Constructs a new FileOutputStream on the file named filename.
FileOutputStream(String filename, boolean append)
Constructs a new FileOutputStream on the file named filename.
Public Methods
void close()
Closes this stream.
FileChannel getChannel()
Returns the FileChannel equivalent to this output stream.
final FileDescriptor getFD()
Returns a FileDescriptor which represents the lowest level representation of an operating system stream resource.
void write(byte[] buffer)
Writes the entire contents of the byte array buffer to this stream.
void write(int oneByte)
Writes the specified byte oneByte to this stream.
void write(byte[] buffer, int offset, int count)
Writes count bytes from the byte array buffer starting at offset to this stream.
Protected Methods
void finalize()
Frees any resources allocated for this stream before it is garbage collected.
[Expand]
Inherited Methods
From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable

Public Constructors

public FileOutputStream (File file)

Since: API Level 1

Constructs a new FileOutputStream on the File file. If the file exists, it is overwritten.

Parameters
file the file to which this stream writes.
Throws
FileNotFoundException if file cannot be opened for writing.
SecurityException if a SecurityManager is installed and it denies the write request.

public FileOutputStream (File file, boolean append)

Since: API Level 1

Constructs a new FileOutputStream on the File file. The parameter append determines whether or not the file is opened and appended to or just opened and overwritten.

Parameters
file the file to which this stream writes.
append indicates whether or not to append to an existing file.
Throws
FileNotFoundException if the file cannot be opened for writing.
SecurityException if a SecurityManager is installed and it denies the write request.

public FileOutputStream (FileDescriptor fd)

Since: API Level 1

Constructs a new FileOutputStream on the FileDescriptor fd. The file must already be open, therefore no FileNotFoundException will be thrown.

Parameters
fd the FileDescriptor to which this stream writes.
Throws
NullPointerException if fd is null.
SecurityException if a SecurityManager is installed and it denies the write request.

public FileOutputStream (String filename)

Since: API Level 1

Constructs a new FileOutputStream on the file named filename. If the file exists, it is overwritten. The filename may be absolute or relative to the system property "user.dir".

Parameters
filename the name of the file to which this stream writes.
Throws
FileNotFoundException if the file cannot be opened for writing.
SecurityException if a SecurityManager is installed and it denies the write request.

public FileOutputStream (String filename, boolean append)

Since: API Level 1

Constructs a new FileOutputStream on the file named filename. The parameter append determines whether or not the file is opened and appended to or just opened and overwritten. The filename may be absolute or relative to the system property "user.dir".

Parameters
filename the name of the file to which this stream writes.
append indicates whether or not to append to an existing file.
Throws
FileNotFoundException if the file cannot be opened for writing.
SecurityException if a SecurityManager is installed and it denies the write request.

Public Methods

public void close ()

Since: API Level 1

Closes this stream. This implementation closes the underlying operating system resources allocated to represent this stream.

Throws
IOException if an error occurs attempting to close this stream.

public FileChannel getChannel ()

Since: API Level 1

Returns the FileChannel equivalent to this output stream.

The file channel is write-only and has an initial position within the file that is the same as the current position of this stream within the file. All changes made to the underlying file descriptor state via the channel are visible by the output stream and vice versa.

Returns
  • the file channel representation for this stream.

public final FileDescriptor getFD ()

Since: API Level 1

Returns a FileDescriptor which represents the lowest level representation of an operating system stream resource.

Returns
  • a FileDescriptor representing this stream.
Throws
IOException if an error occurs attempting to get the FileDescriptor of this stream.

public void write (byte[] buffer)

Since: API Level 1

Writes the entire contents of the byte array buffer to this stream.

Parameters
buffer the buffer to be written to the file.
Throws
IOException if this stream is closed or an error occurs attempting to write to this stream.

public void write (int oneByte)

Since: API Level 1

Writes the specified byte oneByte to this stream. Only the low order byte of the integer oneByte is written.

Parameters
oneByte the byte to be written.
Throws
IOException if this stream is closed an error occurs attempting to write to this stream.

public void write (byte[] buffer, int offset, int count)

Since: API Level 1

Writes count bytes from the byte array buffer starting at offset to this stream.

Parameters
buffer the buffer to write to this stream.
offset the index of the first byte in buffer to write.
count the number of bytes from buffer to write.
Throws
IndexOutOfBoundsException if count < 0 or offset < 0, or if count + offset is greater than the length of buffer.
IOException if this stream is closed or an error occurs attempting to write to this stream.
NullPointerException if buffer is null.

Protected Methods

protected void finalize ()

Since: API Level 1

Frees any resources allocated for this stream before it is garbage collected. This method is called from the Java Virtual Machine.

Throws
IOException if an error occurs attempting to finalize this stream.