Reading files
Read files with FileKit Core in Kotlin Multiplatform
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
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
For text files, you can use the readString()
suspend function, which returns the file contents as a String
:
Using Source
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.