Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# AutoCloseOutputStream

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/android/os/ParcelFileDescriptor.AutoCloseOutputStream "View this page in Java")

```
open class AutoCloseOutputStream : FileOutputStream
```

|---|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||||
| ↳ | [java.io.OutputStream](https://developer.android.com/reference/kotlin/java/io/OutputStream.html#) |||
|   | ↳ | [java.io.FileOutputStream](https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#) ||
|   |   | ↳ | [android.os.ParcelFileDescriptor.AutoCloseOutputStream](https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.AutoCloseOutputStream#) |

|---|
| Known Direct Subclasses [AssetFileDescriptor.AutoCloseOutputStream](https://developer.android.com/reference/kotlin/android/content/res/AssetFileDescriptor.AutoCloseOutputStream.html#) |---|---| | [AssetFileDescriptor.AutoCloseOutputStream](https://developer.android.com/reference/kotlin/android/content/res/AssetFileDescriptor.AutoCloseOutputStream.html#) | An OutputStream you can create on a ParcelFileDescriptor, which will take care of calling android. | |

An OutputStream you can create on a ParcelFileDescriptor, which will take care of calling android.os.ParcelFileDescriptor#close for you when the stream is closed.

## Summary

| Public constructors ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.AutoCloseOutputStream#AutoCloseOutputStream(android.os.ParcelFileDescriptor)(pfd: https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor!)` <br /> |

| Public methods ||
|---|---|
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/os/ParcelFileDescriptor.AutoCloseOutputStream#close()()` Closes this file output stream and releases any system resources associated with this stream. |

| Inherited functions ||
|---|---|
| From class `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#` |---|---| | `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/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. <br /> | | `https://developer.android.com/reference/kotlin/java/nio/channels/FileChannel.html#!` | `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#getChannel()()` Returns the unique `https://developer.android.com/reference/kotlin/java/nio/channels/FileChannel.html#` object associated with this file output stream. The initial `https://developer.android.com/reference/kotlin/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. <br /> | | `https://developer.android.com/reference/kotlin/java/io/FileDescriptor.html#!` | `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#getFD()()` Returns the file descriptor associated with this stream. <br /> | | `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#write(kotlin.ByteArray)(b: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html!)` Writes `b.length` bytes from the specified byte array to this file output stream. <br /> | | `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#write(kotlin.ByteArray,%20kotlin.Int,%20kotlin.Int)(b: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-byte-array/index.html!, off: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, len: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` Writes `len` bytes from the specified byte array starting at offset `off` to this file output stream. <br /> | | `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/java/io/FileOutputStream.html#write(kotlin.Int)(b: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` Writes the specified byte to this file output stream. Implements the `write` method of `OutputStream`. <br /> | ||
| From class `https://developer.android.com/reference/kotlin/java/io/OutputStream.html#` |---|---| | `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/java/io/OutputStream.html#flush()()` Flushes this output stream and forces any buffered output bytes to be written out. The general contract of `flush` is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. The `flush` method of `OutputStream` does nothing. <br /> | | `https://developer.android.com/reference/kotlin/java/io/OutputStream.html#!` | `https://developer.android.com/reference/kotlin/java/io/OutputStream.html#nullOutputStream()()` Returns a new `OutputStream` which discards all bytes. The returned stream is initially open. The stream is closed by calling the `close()` method. Subsequent calls to `close()` have no effect. While the stream is open, the `write(int)`, `write(byte[])`, and `write(byte[], int, int)` methods do nothing. After the stream has been closed, these methods all throw `IOException`. The `flush()` method does nothing. <br /> | ||

## Public constructors

### AutoCloseOutputStream

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
AutoCloseOutputStream(pfd: ParcelFileDescriptor!)
```

## Public methods

### close

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun close(): Unit
```

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.

| Exceptions ||
|---|---|
| `java.lang.Exception` | if this resource cannot be closed |
| `java.io.IOException` | if an I/O error occurs. |