Looking for a library or an alternative for basic multiplatform File I/O

Hi all,

I posted this question recently on the r/Kotlin subreddit (here). Afterwards I thought that I might get better answers here, since I really enjoy this community. I’d be glad if you could help me!

I’m trying to find the best way to interact with a filesystem and do basic operations on it (I/O). My requirements would be:

  • multiplatform
  • (compatible with) Kotlin v1.4.32
  • FS navigation ( Path )
  • Basic I/O (most importantly txt file reading, maybe also writing, creating/deleting)
  • Bonus Points if it can be done nonblockingly

What would not be needed:

  • streaming possibilities
  • random access
  • control over platform-specific details
  • basically anything that is more advanced than basic CRUD

I don’t think it’s a good idea to wait for kotlinx.io *, so what could other alternatives be? The last option would be to implement it with expect / actual for every platform, but maybe there’s already a fitting library for this? Maybe korio? Does anyone have experience with it, is it up-to-date and well usable?

Thanks for your input!

* I get it that they don’t want to waste their resources on something that is low-priority, but I really don’t understand why such an essential thing as multiplatform I/O is considered “low priority”. Sad story.

It seems obvious how the file I/O would be done in Java and Native.

But what about JavaScript? Kotlin/JS runs in a browser usually? How would you suggest to map normal filesystem I/O API into browser environment?

I suspect OP doesn’t need an advanced filesystem API, only basic operations.

Something simple that has JVM actuals using java.nio and maybe a basic virtual filesystem on JS?

1 Like

First of all, thanks for the replies. Indeed, as @arocnies pointed out, I would not need anything “advanced” when it comes to the filesystem API. Basically, my goal with trying to go multiplatform is to be a maximally inclusive library. So when @skylen asks how it should work in a browser env, I have to admit that I have no idea. And that is because I do not intend to use it with Kotlin/JS, but I want to provide this possibilty for others. Considering this, a VFS interface for browser environments would probably be the best choice, so that library users can decide on their own how FS interaction in a browser should look like.

Apart from that the minimal requirements for me would really just be:

  • read text from file
  • adress file by path
  • some path composition utilities
  • NIO

If anyone can recommend a library for that, I’d be thankful. korio seems promising, and I also got okio recommended. Does anyone know these libs and can tell me whether they are proper and also fitting my use case?