Android 從properties配置文件讀取數據
1、Android工程文件項目裏面的assets下新建一個.properties配置文件,命名爲value.properties;
2、在value.properties裏面寫入數據,等號前面是id(string),後面是value值(string),如:
1=1
2=2
3=3
4=4
·······
3、(記得配置文件一定要放在assets下)創建流InputStream用來打開配置文件,load來加載
Properties prop = new Properties();
InputStream in = getApplicationContext().getAssets().open("value.properties");
prop.load( new InputStreamReader(in,"utf-8"));
源碼:
//讀取properties配置文件
try {
InputStream in = getApplicationContext().getAssets().open("value.properties"); //打開assets目錄下的config.properties文件
prop.load( new InputStreamReader(in,"utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
prop.getProperty("1");//根據ID號獲取值