Commit 4bc32780 authored by p x's avatar p x
Browse files

添加assets 工具类

parent 5d52878c
package commons.stand
import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.InputStream
/*****、获取assets文件内容*****/
object FileAssetsIoUtils {
suspend fun getFileDate(context: Context, fileName: String): String {
return withContext(Dispatchers.IO){
val assetManager = context.assets
val inputStream: InputStream = assetManager.open(fileName)
// 方式1:手动逐行读取(推荐,Kotlin简洁写法)
val ret= inputStream.use { ins -> // use 自动关闭流(AutoCloseable)
val size = ins.available()
val bytes = ByteArray(size)
ins.read(bytes)
val str = String(bytes)
return@use str
}
return@withContext ret
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment