read file from assets

前端 未结 18 2232
终归单人心
终归单人心 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:35

    one line solution for kotlin:

    fun readFileText(fileName: String): String {
        return assets.open(fileName).bufferedReader().use { it.readText() }
    }
    

    Also you can use it as extension function everyWhere

    fun Context.readTextFromAsset(fileName : String) : String{
         return assets.open(fileName).bufferedReader().use { 
         it.readText()}
    }
    

    Simply call in any context Class

    context.readTextFromAsset("my file name")
    

提交回复
热议问题