Write files with FileKit Core in Kotlin Multiplatform
FileKit Core provides a consistent API for writing files across all platforms. The PlatformFile
class offers multiple methods to write data to files, from simple strings to binary data and streaming operations.
The most common way to write data to a file is using the write()
infix function with a ByteArray
:
You can also write a string to a file using the writeString()
function:
For more advanced use cases or when working with large files, you can use the sink()
method:
To append data to an existing file instead of overwriting it, use the append
parameter:
You can easily copy the contents of one file to another using the write
infix function with another PlatformFile
or the copyTo
function:
You can move a file from one location to another using the atomicMove
function:
FileKit also provides a method to delete files:
Before writing to a file, you may need to ensure its parent directory exists:
When writing files, you should handle potential errors:
FileKit provides a convenient way to download files in web environments (JS and WASM targets). This functionality allows users to save files from your web application to their local device:
See also: Reading Files for information on how to read files after writing them.
Write files with FileKit Core in Kotlin Multiplatform
FileKit Core provides a consistent API for writing files across all platforms. The PlatformFile
class offers multiple methods to write data to files, from simple strings to binary data and streaming operations.
The most common way to write data to a file is using the write()
infix function with a ByteArray
:
You can also write a string to a file using the writeString()
function:
For more advanced use cases or when working with large files, you can use the sink()
method:
To append data to an existing file instead of overwriting it, use the append
parameter:
You can easily copy the contents of one file to another using the write
infix function with another PlatformFile
or the copyTo
function:
You can move a file from one location to another using the atomicMove
function:
FileKit also provides a method to delete files:
Before writing to a file, you may need to ensure its parent directory exists:
When writing files, you should handle potential errors:
FileKit provides a convenient way to download files in web environments (JS and WASM targets). This functionality allows users to save files from your web application to their local device:
See also: Reading Files for information on how to read files after writing them.