0

I am using IntelliJ for the first time to write a Kotlin program for windows.

I need to read a file so I used code from a sample site:

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

fun main(args: Array<String>) {
  val inputStream: InputStream = File("bezkoder.txt").inputStream()
  val lineList = mutableListOf<String>()

  inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }
  lineList.forEach{println(">  " + it)}
}

The thing is that it doesn't recognise the import for the java classes.

I guess it is something in my setup but I have no idea where to look and haven't managed to find an answer.

This is my SDK setup screen

enter image description here

enter image description here

enter image description here

enter image description here

theblitz
  • 6,683
  • 16
  • 60
  • 114

1 Answers1

1

You can't use Java SDK classes in the Kotlin/Native projects, only in Kotlin/JVM.

Please also make sure you have a valid JDK configuration in the project. See this answer for more details.

If you want to read a file in Kotlin Native, see CsvParser.kt example.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Thanks. My setup seems to be okay. I have updated my question with the configuration screen – theblitz May 02 '20 at 19:23
  • 1
    Please try File | Invalidate Caches | Invalidate and Restart. I [can't reproduce the issue](https://i.imgur.com/Aziv4MI.png). Double check project and module is set to use the same JDK configuration. – CrazyCoder May 02 '20 at 19:26
  • I looked at you link and the one big difference I see is I don't have the KotlinJavaRuntime libraries. I am using KotlinNative - could that be the problem? I have added an image showing what I see. – theblitz May 02 '20 at 19:35
  • 1
    Yes, you can't use Java classes in Native projects. – CrazyCoder May 02 '20 at 20:48
  • 1
    See https://github.com/JetBrains/kotlin-native/blob/master/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt. – CrazyCoder May 03 '20 at 00:39