public class

FileOutputStream

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

Class Overview

An output stream that writes bytes to a file. If the output file exists, it can be replaced or appended to. If it does not exist, a new file will be created.

   File file = ...
   OutputStream out = null;
   try {
     out = new BufferedOutputStream(new FileOutputStream(file));
     ...
    finally {
     if (out != null) {
       out.close();
     }
   }
 }

This stream is not buffered. Most callers should wrap this stream with a BufferedOutputStream.

Use FileWriter to write characters, as opposed to bytes, to a file.

Summary

Public Constructors
FileOutputStream(File file)
Constructs a new FileOutputStream that writes to file.
FileOutputStream(File file, boolean append)
Constructs a new FileOutputStream that writes to file, creating it if necessary.
FileOutputStream(FileDescriptor fd)
Constructs a new FileOutputStream that writes to fd.
FileOutputStream(String path)
Equivalent to new FileOutputStream(new File(path), false).
FileOutputStream(String path, boolean append)
Equivalent to new FileOutputStream(new File(path), append).
Public Methods
void close()
Closes this stream.
FileChannel getChannel()
Returns a write-only FileChannel that shares its position with this stream.
final FileDescriptor getFD()
Returns the underlying file descriptor.
void write(byte[] buffer, int offset, int byteCount)
Writes count bytes from the byte array buffer starting at position offset to this stream.
void write(byte[] buffer)
Writes the entire contents of the byte array buffer to this stream.
void write(int oneByte)
Writes a single byte 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 that writes to file.

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 that writes to file, creating it if necessary. If append is true and the file already exists, it will be appended to. Otherwise a new file will be created.

Parameters
file the file to which this stream writes.
append true 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 that writes to fd.

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 path)

Since: API Level 1

Equivalent to new FileOutputStream(new File(path), false).

public FileOutputStream (String path, boolean append)

Since: API Level 1

Equivalent to new FileOutputStream(new File(path), append).

Public Methods

public void close ()

Since: API Level 1

Closes this stream. Implementations of this method should free any resources used by the stream. This implementation does nothing.

Throws
IOException

public FileChannel getChannel ()

Since: API Level 1

Returns a write-only FileChannel that shares its position with this stream.

public final FileDescriptor getFD ()

Since: API Level 1

Returns the underlying file descriptor.

Throws
IOException

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

Since: API Level 1

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

Parameters
buffer the buffer to be written.
offset the start position in buffer from where to get bytes.
byteCount the number of bytes from buffer to write to this stream.
Throws
IOException

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

public void write (int oneByte)

Since: API Level 1

Writes a single byte to this stream. Only the least significant byte of the integer oneByte is written to the stream.

Parameters
oneByte the byte to be written.
Throws
IOException

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.