read file from assets

前端 未结 18 2245
终归单人心
终归单人心 2020-11-21 22:47
public class Utils {
    public static List getMessages() {
        //File file = new File(\"file:///android_asset/helloworld.txt\");
        AssetMan         


        
18条回答
  •  忘了有多久
    2020-11-21 23:44

    @HpTerm answer Kotlin version:

    private fun getDataFromAssets(activity: Activity): String {

        var bufferedReader: BufferedReader? = null
        var data = ""
    
        try {
            bufferedReader = BufferedReader(
                InputStreamReader(
                    activity?.assets?.open("Your_FILE.html"),
                    "UTF-8"
                )
            )                  //use assets? directly if in activity
    
            var mLine:String? = bufferedReader.readLine()
            while (mLine != null) {
                data+= mLine
                mLine=bufferedReader.readLine()
            }
    
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                bufferedReader?.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return data
    }
    

提交回复
热议问题