[英]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.