A no-nonsense File
API for Kotlin Multiplatform. It gets the job done.
fun main() {
val file = "/path/to/thing.txt".toFile()
assertFalse(file.exists2())
file.parentFile?.mkdirs2(mode = "775", mustCreate = true)
val data = "Hello World!".encodeToByteArray()
// See also openRead, openWrite, openAppend
file.openReadWrite(excl = OpenExcl.MustCreate.of("644")).use { stream ->
assertEquals(0L, stream.size())
assertEquals(0L, stream.postion())
stream.write(data)
assertEquals(data.size.toLong(), stream.size())
assertEquals(data.size.toLong(), stream.position())
val buf = ByteArray(data.size)
stream.position(new = 2L)
stream.read(buf, position = 0L)
assertEquals(2L, stream.position())
assertContentEquals(data, buf)
stream.size(new = 0L).sync(meta = true).write(buf)
}
file.appendBytes(excl = OpenExcl.MustExist, data)
file.chmod2("400")
assertContentEquals(data + data, file.readBytes())
assertEquals("Hello World!Hello World!", file.readUtf8())
file.delete2(ignoreReadOnly = true)
}
dependencies {
implementation("io.matthewnelson.kmp-file:file:0.4.0")
}