Android 筆記:讀取配置文件config.properties

開發中有很多配置需要在配置文件中設置,這樣讀取也方便,修改也方便。

下面就來說一說在Android中怎麼讀取配置文件。

配置文件存放的位置是在/src/main/assets下,這個文件可以手動建也可以系統自己建,推薦大家手動建。


讀取配置文件:

        

/**
 * @param c
 * @param s
 * @return 讀取配置文件 config.properties
 */
public static String getPropertiesURL(Context c, String s) {
    String url = null;
    Properties properties = new Properties();
    try {
        properties.load(c.getAssets().open("config.properties"));
        url = properties.getProperty(s);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return url;

}

我的配置文件中放的是ip和端口,所以在onCreate方法中獲取:

int port = Integer.parseInt(MainActivity.getPropertiesURL(this,"sentport"));
String ip = MainActivity.getPropertiesURL(this,"ip");
這樣就完成了獲取配置文件中值了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章