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

Usage

nonWebMain
Both are suspend functions that switch to Dispatchers.IO. The parent directory must exist: create it first with createDirectories(). Failures throw kotlinx.io.IOException or FileKitException.
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.

Copy another file’s content

write also takes a PlatformFile and streams its content into the destination, without loading it into memory:
nonWebMain
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.

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:
nonWebMain
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.
  • 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.

Platform notes