android讀取配置檔案寫法
阿新 • • 發佈:2019-01-24
在研究兩個人聊天的時候,總需要改登入賬戶和密碼,而且如果伺服器地址變了,還需要改伺服器地址,太苦逼了,索性都弄到配置檔案裡去。
借鑑了兩篇文章:
總結自己的寫法:
<span style="font-size:12px;">public class WeixinUtils { static String pro_value; public static String getProperties(Context c, String proName){ Properties props = new Properties(); try { //方法一:通過activity中的context攻取setting.properties的FileInputStream InputStream in = c.getAssets().open("property.properties"); props.load(in); } catch (Exception e1) { e1.printStackTrace(); } pro_value = props.getProperty(proName); return pro_value; } }</span>
properties檔案放置的位置:
properties中的內容:
專案中讀取屬性的用法:
String loginName = WeixinUtils.getProperties(getApplicationContext(), "login_name")
注意: