File IO with Kotlin multiplatform
Asked Answered
A

3

15

I would like to do some basic filesystem operations on mingwX64 Windows (and possibly other platforms): open, close, read, rename, get metadata, list files in a directory.

I have found one project that promises to implement this functionality: KotlinxIO. However, there has been no progress made in years.

Are there any other alternatives or workarounds?

Avellaneda answered 30/6, 2021 at 8:35 Comment(1)
Note kolinx-io seems to have had some recent development; the latest release, which as of this comment is version 0.3.0, came out September 2023.Jacquetta
A
9

In the end, I used the library Korio. The documentation could be better, but all of the functionality I need is implemented for all platforms (Jvm, Desktop, Android, ..):

import com.soywiz.korio.file.std.*

suspend fun main {
  val cwd = localCurrentDirVfs
  val files = cwd.list()
  cwd["Hello.txt"].open().close()
  cwd["Hello.txt"].renameTo("Hi.txt")
  val metadata = cwd["Hi.txt"].stat()
}

On top of that, it allows usage of the same API for accessing online files, zip archives, etc. which is pretty neat.

Avellaneda answered 5/7, 2021 at 15:47 Comment(0)
B
7

You might want to look at OKIO. There is some multiplatform support, and a Windows target, but I don't know first hand if the filesystem portion is implemented on Windows: https://github.com/square/okio

Beasley answered 30/6, 2021 at 13:49 Comment(0)
V
3

Unfortunately the only workaround right now is to use the expect/actual pattern https://kotlinlang.org/docs/mpp-connect-to-apis.html. Then you can use java.io for Android, Bundle for iOS etc depending on your target platform

Venezuela answered 30/6, 2021 at 9:17 Comment(2)
@Alex.T the answer is correct. java.io should work on windows too, same as any other library you would use in any other java app(google for it), but this code should only be used inside windows part code(actual), to use it from common you need to create expect/actual methods.Embroideress
It does not seem possible to import java.io inside mingwX64Main out of the box.Avellaneda

© 2022 - 2024 — McMap. All rights reserved.