read file from assets

前端 未结 18 2233
终归单人心
终归单人心 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")
    
    0 讨论(0)
  • 2020-11-21 23:36

    It maybe too late but for the sake of others who look for the peachy answers :

    public static String loadAssetFile(Context context, String fileName) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
            StringBuilder out= new StringBuilder();
            String eachline = bufferedReader.readLine();
            while (eachline != null) {
                out.append(eachline);
                eachline = bufferedReader.readLine();
            }
            return out.toString();
        } catch (IOException e) {
            Log.e("Load Asset File",e.toString());
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-11-21 23:42

    cityfile.txt

       public void getCityStateFromLocal() {
            AssetManager am = getAssets();
            InputStream inputStream = null;
            try {
                inputStream = am.open("city_state.txt");
            } catch (IOException e) {
                e.printStackTrace();
            }
            ObjectMapper mapper = new ObjectMapper();
            Map<String, String[]> map = new HashMap<String, String[]>();
            try {
                map = mapper.readValue(getStringFromInputStream(inputStream), new TypeReference<Map<String, String[]>>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            ConstantValues.arrayListStateName.clear();
            ConstantValues.arrayListCityByState.clear();
            if (map.size() > 0)
            {
                for (Map.Entry<String, String[]> e : map.entrySet()) {
                    CityByState cityByState = new CityByState();
                    String key = e.getKey();
                    String[] value = e.getValue();
                    ArrayList<String> s = new ArrayList<String>(Arrays.asList(value));
                    ConstantValues.arrayListStateName.add(key);
                    s.add(0,"Select City");
                    cityByState.addValue(s);
                    ConstantValues.arrayListCityByState.add(cityByState);
                }
            }
            ConstantValues.arrayListStateName.add(0,"Select States");
        }
     // Convert InputStream to String
        public String getStringFromInputStream(InputStream is) {
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return sb + "";
    
        }
    
    0 讨论(0)
  • 2020-11-21 23:44

    If you use other any class other than Activity, you might want to do like,

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( YourApplication.getInstance().getAssets().open("text.txt"), "UTF-8"));
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-21 23:45
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open("helloworld.txt");
    }
    catch (IOException e){
        Log.e("message: ",e.getMessage());
    }
    
    0 讨论(0)
提交回复
热议问题