Hi,
Can you help me find a solution to fix this code?
import java.io.File
fun main() {
val res = "./resources"
val pets = "pets.txt"
val pFile = File("${res}/$pets")
val file = File("${res}/text.txt")
file.forEachLine { println(it) }
val writeFile = File("$res/write.txt")
writeFile.writeText("Kotlin = happiness")
val pList = listOf("cat", "dog", "bird")
pFile.writeText("")
pList.forEach{ pFile.appendText("$it\n")}
val newPets = mutableListOf<String>()
pFile.forEachLine { newPets.add(it) }
println()
println(newPets)
val exists = pFile.exists()
println("\npets exists? $exists")
}
when I try to compile it appears the following error code:
Exception in thread "main" java.io.FileNotFoundException: ./resources/text.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at kotlin.io.FilesKt__FileReadWriteKt.forEachLine(FileReadWrite.kt:190)
at kotlin.io.FilesKt__FileReadWriteKt.forEachLine$default(FileReadWrite.kt:188)
at chapter5._05_03.end.Read_and_write_fileKt.main(read_and_write_file.kt:11)
at chapter5._05_03.end.Read_and_write_fileKt.main(read_and_write_file.kt)
Process finished with exit code 1