简体   繁体   English

分配不是表达式,并且在此上下文中仅允许表达式

[英]Assignments are not expressions, and only expressions are allowed in this context

private fun copyDatabase(context: Context): Boolean {
    try {

        val inputStream = context.assets.open(DatabaseHelper.DBNAME)
        val outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME
        val outputStream = FileOutputStream(outFileName)
        val buff = ByteArray(1024)
        var length = 0

        while((length = inputStream.read(buff)) > 0) {
            outputStream.write(buff , 0 , length)
        }

        outputStream.flush()
        outputStream.close()
        Log.w("MainActivity" , "DB copied")
        return true
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }

}

"while" in this code is not working i need help please 这段代码中的“虽然”不起作用,但我需要帮助

It looks like you're trying to copy the InputStream contents to an OutputStream. 似乎您正在尝试将InputStream内容复制到OutputStream。 For this task you can use InputStream.copyTo extension function in Kotlin: 对于此任务,您可以在Kotlin中使用InputStream.copyTo扩展功能:

// instead of while loop
inputStream.copyTo(outputStream, bufferSize = 1024)

You can fix this like so: 您可以这样解决:

    ...
    var length = inputStream.read(buff)

    while(length  > 0) {
        outputStream.write(buff , 0 , length)
        length = inputStream.read(buff)
    }
    ...  

but @Ilya is right, you are in Kotlin land and be better off using the tools Kotlin provides 但是@Ilya是对的,您在Kotlin的土地上,最好使用Kotlin提供的工具

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配不是表达式,在这种情况下只能使用表达式-Kotlin - Assignments are not expressions and only expressions are allowed in this context - Kotlin 赋值不是表达式,在这种情况下只允许使用表达式 - Kotlin - Assignments are not expressions, and only expressions are allowed in this context - Kotlin 有没有办法解决这个 kotlin 中的“赋值不是表达式,并且在此上下文中只允许表达式” - is there a way for me to fix the " Assignments are not expressions, and only expressions are allowed in this context" in this kotlin if 语句:赋值不是表达式,在这个上下文中只允许表达式 kotlin - If statement: Assignments are not expressions, and only expressions are allowed in this context kotlin 赋值不是表达式,在这个上下文中只允许表达式——如何解决这个错误 - assignments are not expressions, and only expressions are allowed in this context — how to resolve this error 赋值不是表达式,在这个上下文中只允许表达式 kotllin 错误 - Assignments are not expressions, and only expressions are allowed in this context kotllin error 赋值不是表达式,在这个上下文中只允许表达式 Spring Boot Kotllin - Assignments are not expressions, and only expressions are allowed in this context Spring Boot Kotllin 分配不是表达式,在这种情况下只允许使用表达式,我知道它已经讨论过了,但是没有 - Assignments are not expressions, and only expressions are allowed in this context i know its alreaduy disccused but didn't unders 赋值不是表达式,在此上下文中只允许使用表达式 - 将Java转换为Kotlin时出错 - Assignments are not expressions, and only expressions are allowed in this context - Error when convert Java to Kotlin Kotlin/Android 中的“赋值不是表达式”错误 - "Assignments are not expressions" error in Kotlin/Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM