Looking to migrate (with KMP in mind) from using <...
# io
m
Looking to migrate (with KMP in mind) from using java.io.* in my Android app to using either kotlinx.io or okio. I noticed kotlinx.io doesn’t support much in the way of system file operations (like delete). However, with ktor recently switching from okio to kotlinx.io should I consider okio as a no-go and instead place my bets on kotlinx.io with anticipation of upcoming expanded functionality. I’m not in any huge rush.
m
I burned my fingers with kotlinx.io and partly had to switch back to Okio. Conceptually they are very similar but at the moment Okio still provides a lot more functionality. I’d recommend to start with Okio and later switch to kotlinx.io if they are more feature-complete. If, however, you only have very basic IO requirements, you could go with kotlinx.io right away. (kotlinx.io does provide a function to delete a file though: https://github.com/Kotlin/kotlinx-io/blob/master/core/common/src/files/FileSystem.kt)
m
Thanks, yes I see it supports a lot more than chat gpt originally implied. I do find myself writing a lot of extension functions though. e.g.
Path.delete()
m
So do I 😉.
Copy code
fun kotlinx.io.files.Path.toOkioPath(): okio.Path = this.toString().toPath()

fun kotlinx.io.files.Path.writeString(content: String) = SystemFileSystem.sink(this).buffered().use { it.writeString(content) }
fun kotlinx.io.files.Path.writeBytes(content: ByteArray) = SystemFileSystem.sink(this).buffered().use { it.write(content) }

fun kotlinx.io.files.Path.readString(): String = SystemFileSystem.source(this).buffered().use { return it.readString() }
fun kotlinx.io.files.Path.readBytes(): ByteArray = SystemFileSystem.source(this).buffered().use { return it.readByteArray() }
👍 1
m
Are these valid? Just curious if I might be missing something in the null case.
Copy code
fun Path.isDirectory(): Boolean = SystemFileSystem.metadataOrNull(this)?.isDirectory == true

fun Path.isFile(): Boolean = SystemFileSystem.metadataOrNull(this)?.isRegularFile == true
m
It probably works but I prefer this variant:
Copy code
fun Path.isDirectory(): Boolean = SystemFileSystem.metadataOrNull(this)?.isDirectory ?: false
fun Path.isFile(): Boolean = SystemFileSystem.metadataOrNull(this)?.isRegularFile ?: false
👍 1
m
Agreed, the null case default is clearer