# FileOutputStream

public class **FileOutputStream** extends [OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html)  
A file output stream is an output stream for writing data to a
`File` or to a `FileDescriptor`. Whether or not
a file is available or may be created depends upon the underlying
platform. Some platforms, in particular, allow a file to be opened
for writing by only one FileOutputStream (or other
file-writing object) at a time. In such situations the constructors in
this class will fail if the file involved is already open.

`FileOutputStream` is meant for writing streams of raw bytes
such as image data. For writing streams of characters, consider using
`FileWriter`.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileInputStream.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Files.html#newOutputStream(java.nio.file.Path,%20java.nio.file.OpenOption...)`

### Public Constructor Summary

|---|---|
|   | [FileOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#FileOutputStream(java.lang.String))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) name) Creates a file output stream to write to the file with the specified name. |
|   | [FileOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#FileOutputStream(java.lang.String,%20boolean))([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) name, boolean append) Creates a file output stream to write to the file with the specified name. |
|   | [FileOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#FileOutputStream(java.io.File))([File](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html) file) Creates a file output stream to write to the file represented by the specified `File` object. |
|   | [FileOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#FileOutputStream(java.io.File,%20boolean))([File](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html) file, boolean append) Creates a file output stream to write to the file represented by the specified `File` object. |
|   | [FileOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#FileOutputStream(java.io.FileDescriptor))([FileDescriptor](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html) fdObj) Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. |

### Public Method Summary

|---|---|
| void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#close())() Closes this file output stream and releases any system resources associated with this stream. |
| [FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html) | [getChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#getChannel())() Returns the unique `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html` object associated with this file output stream. |
| final [FileDescriptor](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html) | [getFD](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#getFD())() Returns the file descriptor associated with this stream. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#write(byte[]))(byte\[\] b) Writes `b.length` bytes from the specified byte array to this file output stream. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#write(byte[],%20int,%20int))(byte\[\] b, int off, int len) Writes `len` bytes from the specified byte array starting at offset `off` to this file output stream. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#write(int))(int b) Writes the specified byte to this file output stream. |

### Protected Method Summary

|---|---|
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileOutputStream.html#finalize())() Cleans up the connection to the file, and ensures that the `close` method of this file output stream is called when there are no more references to this stream. |

### Inherited Method Summary

From class [java.io.OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html)

|---|---|
| void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html#close())() Closes this output stream and releases any system resources associated with this stream. |
| void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html#flush())() Flushes this output stream and forces any buffered output bytes to be written out. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html#write(byte[]))(byte\[\] b) Writes `b.length` bytes from the specified byte array to this output stream. |
| void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html#write(byte[],%20int,%20int))(byte\[\] b, int off, int len) Writes `len` bytes from the specified byte array starting at offset `off` to this output stream. |
| abstract void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html#write(int))(int b) Writes the specified byte to this output stream. |

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.io.Closeable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |

From interface [java.io.Flushable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Flushable.html)

|---|---|
| abstract void | [flush](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Flushable.html#flush())() Flushes this stream by writing any buffered output to the underlying stream. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Public Constructors

#### public
**FileOutputStream**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) name)

Creates a file output stream to write to the file with the
specified name. A new `FileDescriptor` object is
created to represent this file connection.


First, if there is a security manager, its `checkWrite`
method is called with `name` as its argument.


If the file exists but is a directory rather than a regular file, does
not exist but cannot be created, or cannot be opened for any other
reason then a `FileNotFoundException` is thrown.

##### Parameters

| name | the system-dependent filename |
|---|---|

##### Throws

| [FileNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileNotFoundException.html) | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and its `checkWrite` method denies write access to the file. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)`

#### public
**FileOutputStream**
([String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) name, boolean append)

Creates a file output stream to write to the file with the specified
name. If the second argument is `true`, then
bytes will be written to the end of the file rather than the beginning.
A new `FileDescriptor` object is created to represent this
file connection.


First, if there is a security manager, its `checkWrite`
method is called with `name` as its argument.


If the file exists but is a directory rather than a regular file, does
not exist but cannot be created, or cannot be opened for any other
reason then a `FileNotFoundException` is thrown.

##### Parameters

| name | the system-dependent file name |
| append | if `true`, then bytes will be written to the end of the file rather than the beginning |
|---|---|

##### Throws

| [FileNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileNotFoundException.html) | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason. |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and its `checkWrite` method denies write access to the file. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)`

#### public
**FileOutputStream**
([File](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html) file)

Creates a file output stream to write to the file represented by
the specified `File` object. A new
`FileDescriptor` object is created to represent this
file connection.


First, if there is a security manager, its `checkWrite`
method is called with the path represented by the `file`
argument as its argument.


If the file exists but is a directory rather than a regular file, does
not exist but cannot be created, or cannot be opened for any other
reason then a `FileNotFoundException` is thrown.

##### Parameters

| file | the file to be opened for writing. |
|---|---|

##### Throws

| [FileNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileNotFoundException.html) | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and its `checkWrite` method denies write access to the file. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html#getPath()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)`

#### public
**FileOutputStream**
([File](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html) file, boolean append)

Creates a file output stream to write to the file represented by
the specified `File` object. If the second argument is
`true`, then bytes will be written to the end of the file
rather than the beginning. A new `FileDescriptor` object is
created to represent this file connection.


First, if there is a security manager, its `checkWrite`
method is called with the path represented by the `file`
argument as its argument.


If the file exists but is a directory rather than a regular file, does
not exist but cannot be created, or cannot be opened for any other
reason then a `FileNotFoundException` is thrown.

##### Parameters

| file | the file to be opened for writing. |
| append | if `true`, then bytes will be written to the end of the file rather than the beginning |
|---|---|

##### Throws

| [FileNotFoundException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileNotFoundException.html) | if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | if a security manager exists and its `checkWrite` method denies write access to the file. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/File.html#getPath()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)`

#### public
**FileOutputStream**
([FileDescriptor](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html) fdObj)

Creates a file output stream to write to the specified file
descriptor, which represents an existing connection to an actual
file in the file system.


First, if there is a security manager, its `checkWrite`
method is called with the file descriptor `fdObj`
argument as its argument.


If `fdObj` is null then a `NullPointerException`
is thrown.


This constructor does not throw an exception if `fdObj`
is `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html#valid()`.
However, if the methods are invoked on the resulting stream to attempt
I/O on the stream, an `IOException` is thrown.

##### Parameters

| fdObj | the file descriptor to be opened for writing |
|---|---|

## Public Methods

#### public void
**close**
()

Closes this file output stream and releases any system resources
associated with this stream. This file output stream may no longer
be used for writing bytes.

If this stream has an associated channel then the channel is closed
as well.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

#### public [FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html)
**getChannel**
()

Returns the unique `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html`
object associated with this file output stream.

The initial `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html#position()` of the returned channel will be equal to the
number of bytes written to the file so far unless this stream is in
append mode, in which case it will be equal to the size of the file.
Writing bytes to this stream will increment the channel's position
accordingly. Changing the channel's position, either explicitly or by
writing, will change this stream's file position.

##### Returns

- the file channel associated with this file output stream

#### public final [FileDescriptor](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html)
**getFD**
()

Returns the file descriptor associated with this stream.

##### Returns

- the `FileDescriptor` object that represents the connection to the file in the file system being used by this `FileOutputStream` object.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileDescriptor.html`

#### public void
**write**
(byte\[\] b)

Writes `b.length` bytes from the specified byte array
to this file output stream.

##### Parameters

| b | the data. |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

#### public void
**write**
(byte\[\] b, int off, int len)

Writes `len` bytes from the specified byte array
starting at offset `off` to this file output stream.

##### Parameters

| b | the data. |
| off | the start offset in the data. |
| len | the number of bytes to write. |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

#### public void
**write**
(int b)

Writes the specified byte to this file output stream. Implements
the `write` method of `OutputStream`.

##### Parameters

| b | the byte to be written. |
|---|---|

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

## Protected Methods

#### protected void
**finalize**
()

Cleans up the connection to the file, and ensures that the
`close` method of this file output stream is
called when there are no more references to this stream.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | if an I/O error occurs. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/FileInputStream.html#close()`