Where to put own properties file in an android project created with Android Studio?

后端 未结 4 1739
别跟我提以往
别跟我提以往 2021-01-01 19:50

I just started to use Android Studio. I don\'t know where to put my own properties file since there is no assets folder in the project structure.

The posted snippet

4条回答
  •  死守一世寂寞
    2021-01-01 20:21

    You can create your asset subfolder in your main folder and then insert the .properties file in it.

    Then, create a class to open and read the file, for example:

    public class PropertiesReader { 
    
     private Context context;
     private Properties properties;
     public PropertiesReader(Context context) { 
      this.context = context;
       //creates a new object ‘Properties’
       properties = new Properties();
    
       public Properties getProperties(String FileName) {
       try {    
        //access to the folder ‘assets’ 
        AssetManager am = context.getAssets(); 
        //opening the file    
        InputStream inputStream = am.open(FileName); 
        //loading of the properties 
        properties.load(inputStream);
       } 
       catch (IOException e) { 
        Log.e("PropertiesReader",e.toString());
       }
      }
      return properties;
     } 
    }
    

    For more information, see http://pillsfromtheweb.blogspot.it/2014/09/properties-file-in-android.html

提交回复
热议问题