Skip to main content
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

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 RawSource, so you can process the file in chunks without loading it at once. It is not available on the web.
nonWebMain
source() does blocking I/O: call it on Dispatchers.IO, and close it with use.

Handle errors

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