> ## 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.

# Reading files

> Read a PlatformFile as bytes, as text, or as a kotlinx-io stream

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

`readBytes()` and `readString()` load the whole file into memory and return a `ByteArray` or a UTF-8 `String`. For large files, stream them with `source()` instead.

## Usage

```kotlin theme={null}
val bytes: ByteArray = file.readBytes()
val text: String = file.readString()
```

Both are suspend functions. On every platform except the web, they switch to `Dispatchers.IO` themselves.

## Stream a large file

`source()` returns a [kotlinx-io](https://github.com/Kotlin/kotlinx-io) `RawSource`, so you can process the file in chunks without loading it at once. It is not available on the web.

```kotlin nonWebMain theme={null}
suspend fun countLines(file: PlatformFile): Int = withContext(Dispatchers.IO) {
    file.source().buffered().use { source ->
        var count = 0
        while (source.readLine() != null) count++
        count
    }
}
```

`source()` does blocking I/O: call it on `Dispatchers.IO`, and close it with `use`.

## Handle errors

```kotlin theme={null}
val text: String? = try {
    file.readString()
} catch (e: FileNotFoundException) { // kotlinx.io.files
    null
}
```

A missing file throws `FileNotFoundException`, other I/O failures throw `kotlinx.io.IOException`. FileKit's own errors, such as reading a directory or a `content://` Uri that can't be opened, throw `FileKitException`.

## Platform notes

| Platform | Behavior |
| - | - |
| Android | `content://` files are read through the `ContentResolver`. |
| iOS, macOS | Security-scoped access is started and stopped for you, also for `source()` until you close it. |
| Web | Picked files are read with a `FileReader`. Files in the origin private file system are read at their current content. See [Files on the web](https://filekit.mintlify.site/core/web.md). |

## Related

* [Writing files](https://filekit.mintlify.site/core/write-file.md)
* [PlatformFile](https://filekit.mintlify.site/core/platform-file.md): name, size, and other properties


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