标签:span override 文件 rri tar put files flush over
第一步:打开一个异步线程
Thread(object : Runnable { override fun run() { //读取与写入 setScript() val msg = Message.obtain() msg.obj = "信息" //返回主线程 myHandler.sendMessage(msg) } }).start()
第二步:setScript()
fun setScript(){ //读取assets中的文件 var inputStream: InputStream = assets.open("script.pck") //保存到手机的路劲 val appDir = File(fileStr1 + "/asset") if (!appDir.exists()) { appDir.mkdir() } val file = File(appDir, "script.pck") val fos = FileOutputStream(file) var bytes: ByteArray = ByteArray(1024) var byteCount: Int = inputStream.read(bytes) while (byteCount !== -1) { fos.write(bytes, 0, byteCount) byteCount = inputStream.read(bytes) } fos.flush() inputStream.close() fos.close() }
以上就是Kotlin启动异步线程读取和写入文件。
Kotlin 的子线程从asstes中读取文件保存到手机本地
标签:span override 文件 rri tar put files flush over
原文地址:https://www.cnblogs.com/niupi/p/11647928.html