> ## Documentation Index
> Fetch the complete documentation index at: https://filekit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing files

> Write bytes, text, or a kotlinx-io stream to a PlatformFile

[See the platform support matrix](https://filekit.mintlify.site/platform-support.md#filekit-core)

`write()` and `writeString()` replace the content of a file, creating it if needed. For large or incremental writes, stream with `sink()`.

## Usage

```kotlin nonWebMain theme={null}
val file = FileKit.filesDir / "notes.txt"

file.writeString("Hello, FileKit!")
file.write("Hello again".encodeToByteArray())
```

Both are suspend functions that switch to `Dispatchers.IO`. The parent directory must exist: create it first with [`createDirectories()`](https://filekit.mintlify.site/core/file-operations#create-directories). Failures throw `kotlinx.io.IOException` or `FileKitException`.

> [!NOTE]
>
> These functions are not declared in `commonMain`. Call them from `nonWebMain` or a platform source set. The web has separate, experimental versions for the origin private file system: see [Write on the web](#write-on-the-web).

## Copy another file's content

`write` also takes a `PlatformFile` and streams its content into the destination, without loading it into memory:

```kotlin nonWebMain theme={null}
val destination = FileKit.filesDir / "copy.pdf"
destination write pickedFile
```

This works when the source size is unknown, as with some Android `content://` files. It throws if the source is a directory or the same file as the destination. To copy into a folder or copy a whole tree, see [File operations](https://filekit.mintlify.site/core/file-operations.md#copy-a-file).

## Stream with a sink

`sink()` returns a kotlinx-io `RawSink`. Pass `append = true` to add to the end of the file instead of replacing it:

```kotlin nonWebMain theme={null}
suspend fun appendLog(file: PlatformFile, line: String) = withContext(Dispatchers.IO) {
    file.sink(append = true).buffered().use { sink ->
        sink.writeString("$line\n")
    }
}
```

`sink()` does blocking I/O: call it on `Dispatchers.IO`, and close it with `use` so the data is flushed.

## Write on the web

Browsers don't let a page write to the user's files. On JS and Wasm you have two options:

* Give the user a file with `FileKit.download(bytes, fileName)`: see [Download files](https://filekit.mintlify.site/core/web.md#download-files).
* Store files in the browser's private storage with the experimental origin private file system API, which provides its own `write`, `writeString`, and `delete`: see [Files on the web](https://filekit.mintlify.site/core/web.md#store-files-in-the-origin-private-file-system).

## Platform notes

| Platform | Behavior |
| - | - |
| Android | Writing to a `content://` file truncates it first (`append = false`), including on providers such as Google Drive. A file you create with `/` inside a picked folder is created as a new document. |
| iOS, macOS | Security-scoped access is started and stopped for you, also for `sink()` until you close it. |
| Web | Only origin private file system files can be written. Writing to a picked file throws. |

## Related

* [Reading files](https://filekit.mintlify.site/core/read-file.md)
* [File operations](https://filekit.mintlify.site/core/file-operations.md): copy, move, delete, and create directories
* [File saver](https://filekit.mintlify.site/dialogs/file-saver.md): let the user choose where to save


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.