第一步:打开一个异步线程
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启动异步线程读取和写入文件。
原文地址:https://www.cnblogs.com/niupi/p/11647928.html
时间: 2024-10-09 11:13:26