Android 下讀取Assets Properties操作封裝工具類
發布時間:2018-06-03作者:laosun閱讀(2081)
為了方便使用,首先創建BaseApplication類,如下所示:import?android.app.Application;
import?android.content.Context;
/**
*?Created?by?sun?on?2018/5/28.
*/
public?class?BaseApplication?extends?Application?{
private?static?Context?mContext;
@Override
public?void?onCreate()?{
super.onCreate();
mContext?=?getApplicationContext();
}
public?static?Context?getInstance()?{
return?mContext;
}
}
創建Prop工具類
import?com.sunjs.application.BaseApplication;
import?com.sunjs.log.LOG;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.util.Properties;
public?class?Prop?{
private?Properties?properties?=?null;
public?Prop(String?fileName,?String?encoding)?{
InputStream?inputStream?=?null;
try?{
inputStream?=?BaseApplication.getInstance().getAssets().open(fileName);
if?(inputStream?==?null)?{
LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),?"Properties?file?not?found?in?classpath:?"?+?fileName);
}
properties?=?new?Properties();
properties.load(new?InputStreamReader(inputStream,?encoding));
}?catch?(IOException?e)?{
LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),e,"Error?loading?properties?file.");
}?finally?{
if?(inputStream?!=?null)?try?{
inputStream.close();
}?catch?(IOException?e)?{
LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),e,?e.getMessage());
}
}
}
private?ClassLoader?getClassLoader()?{
ClassLoader?ret?=?Thread.currentThread().getContextClassLoader();
return?ret?!=?null???ret?:?getClass().getClassLoader();
}
public?String?get(String?key)?{
return?properties.getProperty(key);
}
public?String?get(String?key,?String?defaultValue)?{
return?properties.getProperty(key,?defaultValue);
}
public?Integer?getInt(String?key)?{
return?getInt(key,?null);
}
public?Integer?getInt(String?key,?Integer?defaultValue)?{
String?value?=?properties.getProperty(key);
if?(value?!=?null)?{
return?Integer.parseInt(value.trim());
}
return?defaultValue;
}
public?Long?getLong(String?key)?{
return?getLong(key,?null);
}
public?Long?getLong(String?key,?Long?defaultValue)?{
String?value?=?properties.getProperty(key);
if?(value?!=?null)?{
return?Long.parseLong(value.trim());
}
return?defaultValue;
}
public?Boolean?getBoolean(String?key)?{
return?getBoolean(key,?null);
}
public?Boolean?getBoolean(String?key,?Boolean?defaultValue)?{
String?value?=?properties.getProperty(key);
if?(value?!=?null)?{
value?=?value.toLowerCase().trim();
if?("true".equals(value))?{
return?true;
}?else?if?("false".equals(value))?{
return?false;
}
throw?new?RuntimeException("The?value?can?not?parse?to?Boolean?:?"?+?value);
}
return?defaultValue;
}
public?boolean?containsKey(String?key)?{
return?properties.containsKey(key);
}
public?Properties?getProperties()?{
return?properties;
}
}
創建讀取操作類
import?java.util.concurrent.ConcurrentHashMap;
/**
*?Created?by?sun?on?2018/5/28.
*/
public?class?PropKit?{
private?static?Prop?prop?=?null;
private?static?final?ConcurrentHashMap?map?=?new?ConcurrentHashMap();
private?PropKit()?{
}
public?static?Prop?use(String?fileName)?{
return?use(fileName,?Const.DEFAULT_ENCODING);
}
private?static?Prop?use(String?fileName,?String?encoding)?{
Prop?result?=?map.get(fileName);
if?(result?==?null)?{
//服務端并發時使用,這塊其實不用使用
synchronized?(PropKit.class)?{
result?=?map.get(fileName);
if?(result?==?null)?{
result?=?new?Prop(fileName,?encoding);
map.put(fileName,?result);
if?(PropKit.prop?==?null)?{
PropKit.prop?=?result;
}
}
}
}
return?result;
}
public?static?void?clear()?{
prop?=?null;
map.clear();
}
public?static?Prop?getProp()?{
if?(prop?==?null)?{
//默認加載config.properties文件
//這里的Const.default_config就是assets目錄下的config.properties
use(Const.default_config);
}
return?prop;
}
public?static?Prop?getProp(String?fileName)?{
return?map.get(fileName);
}
public?static?String?get(String?key)?{
return?getProp().get(key);
}
public?static?String?get(String?key,?String?defaultValue)?{
return?getProp().get(key,?defaultValue);
}
public?static?Integer?getInt(String?key)?{
return?getProp().getInt(key);
}
public?static?Integer?getInt(String?key,?Integer?defaultValue)?{
return?getProp().getInt(key,?defaultValue);
}
public?static?Long?getLong(String?key)?{
return?getProp().getLong(key);
}
public?static?Long?getLong(String?key,?Long?defaultValue)?{
return?getProp().getLong(key,?defaultValue);
}
public?static?Boolean?getBoolean(String?key)?{
return?getProp().getBoolean(key);
}
public?static?Boolean?getBoolean(String?key,?Boolean?defaultValue)?{
return?getProp().getBoolean(key,?defaultValue);
}
public?static?boolean?containsKey(String?key)?{
return?getProp().containsKey(key);
}
}
測試:
assets下有config.properties
內容如下:
login.url=
dev.mode=true
使用獲取方式如下:
String?loginUrl?=?PropKit.use("config.properties").get("login.url");
boolean?devMode?=?PropKit.getBoolean("dev.mode");
從上邊可以看出,.use是填寫的assets根目錄下的properties文件名稱,代碼中默認的文件名叫做config.properties,所以也可以不用寫.use,可以直接進行獲取。
0 +1
版權聲明
發表評論
請文明留言
發表
共 0 條評論