Skip to main content

Introduction

FileKit Core provides a consistent API for reading files across all platforms. The PlatformFile class offers multiple methods to read the contents of a file, from simple byte arrays to more complex streaming operations.

Reading as ByteArray

Supported on Android, iOS, macOS, JVM, JS and WASM targets
The most common way to read a file is to use the readBytes() suspend function, which returns the file contents as a ByteArray:

Reading as String

Supported on Android, iOS, macOS, JVM, JS and WASM targets
For text files, you can use the readString() suspend function, which returns the file contents as a String:

Using Source

Supported on Android, iOS, macOS, JVM targets
For more advanced use cases or when working with large files, you can use the source() method from kotlinx-io to get a raw source:

Error Handling

When reading files, you should handle potential errors:

Example: Reading Different File Types

See also: Writing Files for information on how to write to files after reading them.