Kotlin can't read a file without java libs?

I need to build a heavy duty API REST and was searching for a very fast programming language.
Something like java, Typed OOP language, don’t like C# because i don’t like M$crosoft policy and we use Linux and postgres extensive in the business.

On the search found Kotlin is more modern and blessed by android.

Read the basic, some videos and want to try it, lets try some easy task first i say, to test this beautiful “not so new” language.

The task: Read a text file line by line.

My surprise was incredible that every tutorial on internet say

import java.io.File
import java.io.InputStream

can’t understand, why import java for “inter-op” for an almost “trivial” job so i check the docs, can’t get info about so came here to ask to try to understand why kotlin don’t have something like kotlin.io.File

Can you guide me please?

This is because Kotlin is multiplatform, can be compiled into multiple much different targets: JVM, JavaScript, native. For each platform the standard library and capabilities are much different, so Kotlin doesn’t try to create a common abstraction, but instead, allows to use the target stdlib instead. We can still create our own abstractions, and often such abstractions are provided as external libraries. For files I/O you can check okio. It is multiplatform and I see classes like FileSystem and Path, so it looks promising, although I never used it myself.

But generally, if you don’t plan to run your REST server on an iPhone ;-), then you can safely assume your service is a JVM service, so there is nothing wrong in using Java stdlib as tutorials suggested.

There is also one additional reason why Kotlin stdlib is rather small and limited. As Kotlin is compiled to run on other platforms (JVM/JavaScript), its stdlib has to be distributed with the application as a library. This is much different than stdlibs of other languages which are usually installed on the target platform. We don’t want our hello world app to weight 100MB just because we used Kotlin. Maybe for this reason even Kotlin authors provide many functionalities as external libraries, often named kotlinx-*. As a matter of fact, there was such library for I/O, but it has been abandoned: GitHub - Kotlin/kotlinx-io: Kotlin multiplatform I/O library

1 Like