package com.base.scanqrclear.utils

import android.content.Context
import java.io.IOException
import java.nio.charset.StandardCharsets


class AssetReader {

    fun readJsonFromAssets(context: Context, fileName: String): String {

        var jsonString = ""
        try {
            // 获取 AssetManager
            val assetManager = context.assets


            // 打开文件并读取为 InputStream
            val inputStream = assetManager.open(fileName)


            // 获取文件大小
            val size = inputStream.available()


            // 创建一个字节数组来存储文件内容
            val buffer = ByteArray(size)


            // 读取文件内容到字节数组
            inputStream.read(buffer)


            // 关闭 InputStream
            inputStream.close()


            // 将字节数组转换为字符串
            jsonString = String(buffer, StandardCharsets.UTF_8)
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return jsonString
    }
}