Tag:

Kotlin readBytes()永远不会完成

我只是试图通过蓝牙套接字来读写,但是我的readBytes调用没有完成。 我认为这是非常简单的,但也许我只是使用错误types的流或东西。 截至目前我的代码只是发送少量的文字作为字节。 这是占位符代码,将被替换为通过流写入和读取文件的代码。 这是我的接收线程: class ReceiveThread(val inStream:BufferedInputStream):Thread() { var bytes:ByteArray? = null override fun run() { BluetoothService.log(“Begin ${BTS_Constants.THREAD_RECEIVE}”) BluetoothService.log(“preparing to read data”) name = BTS_Constants.THREAD_RECEIVE //here is my new code inStream.use { do{ count++ BluetoothService.log(“read loop round $count”) byteList.add(it.read() as Byte) }while (it.available()>0) } BluetoothService.log(“data read: ${byteList.get(0) as Char}”) } } 这是我的发送主题: class SendThread(val […]

在kotlin中处理流

Kotlin如何运行以下代码? 将5000000个整数的集合创建为临时集合还是将filter将其结果立即送到forEach ,这意味着只有20个整数将被查看? 如果不是,我将如何能够避免中间收集? 码: class Tests { @Test fun test() { var counter = 0 (1..10_000_000).filter { it % 2 == 1 }.forEach { counter++ if (counter > 10) return } } }

Kotlin和Gradle – 从stdio读取

我正在尝试使用以下命令来执行我的Kotlin类: ./gradlew -q run < src/main/kotlin/samples/input.txt 这是我的HelloWorld.kt类: package samples fun main(args: Array) { println(“Hello, world!”) val lineRead = readLine() println(lineRead) } 这是我的build.gradle.kts : plugins { kotlin(“jvm”) application } application { mainClassName = “samples.HelloWorldKt” } dependencies { compile(kotlin(“stdlib”)) } repositories { jcenter() } 代码执行,但input.txt文件内包含的数据不显示。 这是我得到的输出: Hello, world! null 我希望能够执行上面的gradlew命令,并将input.txt流重定向到stdio。 我可以很容易地在C ++中做到这一点。 一旦我编译我的.cpp文件,我可以运行: ./my_code < input.txt 并按预期执行。 […]