kotlin
How to read plain text file in kotlin?
1. Using BufferedReader import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val bufferedReader: BufferedReader = File(“example.txt”).bufferedReader() val inputString = bufferedReader.use { it.readText() } println(inputString) } 2. Using InputStream Read By Line import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File(“example.txt”).inputStream() val lineList = mutableListOf<String>() inputStream.bufferedReader().forEachLine { lineList.add(it) } lineList.forEach{println(“> ” + … Read more
Getting “Suspension functions can be called only within coroutine body” when calling withContext(Dispatchers.Main) inside a lambda
You can rewrite your bar function like this: fun bar(completion: () -> Unit) { GlobalScope.launch(Dispatchers.IO) { suspendCoroutine<Unit> { val lambda = { it.resume(Unit) } foo(lambda) } withContext(Dispatchers.Main) { completion() } } }
How do I implement Serializable in Kotlin so it also works in Java?
do I just import java.io.Serializable Yes. Just be aware that Kotlin uses @Transient annotation instead of a keyword. Of course, Java serialization does have its issues, but there’s no difference in that respect between Kotlin and Java, and if you are happy with your current code…