1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > android文件读取工具类 Android 下读取Assets Properties操作封装工具类

android文件读取工具类 Android 下读取Assets Properties操作封装工具类

时间:2024-03-07 00:45:04

相关推荐

android文件读取工具类 Android 下读取Assets Properties操作封装工具类

Android 下读取Assets Properties操作封装工具类

发布时间:-06-03作者:laosun阅读(2081)

为了方便使用,首先创建BaseApplication类,如下所示:importandroid.app.Application;

importandroid.content.Context;

/**

*Createdbysunon/5/28.

*/

publicclassBaseApplicationextendsApplication{

privatestaticContextmContext;

@Override

publicvoidonCreate(){

super.onCreate();

mContext=getApplicationContext();

}

publicstaticContextgetInstance(){

returnmContext;

}

}

创建Prop工具类

importcom.sunjs.application.BaseApplication;

importcom.sunjs.log.LOG;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.util.Properties;

publicclassProp{

privatePropertiesproperties=null;

publicProp(StringfileName,Stringencoding){

InputStreaminputStream=null;

try{

inputStream=BaseApplication.getInstance().getAssets().open(fileName);

if(inputStream==null){

LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),"Propertiesfilenotfoundinclasspath:"+fileName);

}

properties=newProperties();

properties.load(newInputStreamReader(inputStream,encoding));

}catch(IOExceptione){

LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),e,"Errorloadingpropertiesfile.");

}finally{

if(inputStream!=null)try{

inputStream.close();

}catch(IOExceptione){

LOG.e(Thread.currentThread().getStackTrace()[1].getClassName(),e,e.getMessage());

}

}

}

privateClassLoadergetClassLoader(){

ClassLoaderret=Thread.currentThread().getContextClassLoader();

returnret!=null?ret:getClass().getClassLoader();

}

publicStringget(Stringkey){

returnproperties.getProperty(key);

}

publicStringget(Stringkey,StringdefaultValue){

returnproperties.getProperty(key,defaultValue);

}

publicIntegergetInt(Stringkey){

returngetInt(key,null);

}

publicIntegergetInt(Stringkey,IntegerdefaultValue){

Stringvalue=properties.getProperty(key);

if(value!=null){

returnInteger.parseInt(value.trim());

}

returndefaultValue;

}

publicLonggetLong(Stringkey){

returngetLong(key,null);

}

publicLonggetLong(Stringkey,LongdefaultValue){

Stringvalue=properties.getProperty(key);

if(value!=null){

returnLong.parseLong(value.trim());

}

returndefaultValue;

}

publicBooleangetBoolean(Stringkey){

returngetBoolean(key,null);

}

publicBooleangetBoolean(Stringkey,BooleandefaultValue){

Stringvalue=properties.getProperty(key);

if(value!=null){

value=value.toLowerCase().trim();

if("true".equals(value)){

returntrue;

}elseif("false".equals(value)){

returnfalse;

}

thrownewRuntimeException("ThevaluecannotparsetoBoolean:"+value);

}

returndefaultValue;

}

publicbooleancontainsKey(Stringkey){

returnproperties.containsKey(key);

}

publicPropertiesgetProperties(){

returnproperties;

}

}

创建读取操作类

importjava.util.concurrent.ConcurrentHashMap;

/**

*Createdbysunon/5/28.

*/

publicclassPropKit{

privatestaticPropprop=null;

privatestaticfinalConcurrentHashMapmap=newConcurrentHashMap();

privatePropKit(){

}

publicstaticPropuse(StringfileName){

returnuse(fileName,Const.DEFAULT_ENCODING);

}

privatestaticPropuse(StringfileName,Stringencoding){

Propresult=map.get(fileName);

if(result==null){

//服务端并发时使用,这块其实不用使用

synchronized(PropKit.class){

result=map.get(fileName);

if(result==null){

result=newProp(fileName,encoding);

map.put(fileName,result);

if(PropKit.prop==null){

PropKit.prop=result;

}

}

}

}

returnresult;

}

publicstaticvoidclear(){

prop=null;

map.clear();

}

publicstaticPropgetProp(){

if(prop==null){

//默认加载config.properties文件

//这里的Const.default_config就是assets目录下的config.properties

use(Const.default_config);

}

returnprop;

}

publicstaticPropgetProp(StringfileName){

returnmap.get(fileName);

}

publicstaticStringget(Stringkey){

returngetProp().get(key);

}

publicstaticStringget(Stringkey,StringdefaultValue){

returngetProp().get(key,defaultValue);

}

publicstaticIntegergetInt(Stringkey){

returngetProp().getInt(key);

}

publicstaticIntegergetInt(Stringkey,IntegerdefaultValue){

returngetProp().getInt(key,defaultValue);

}

publicstaticLonggetLong(Stringkey){

returngetProp().getLong(key);

}

publicstaticLonggetLong(Stringkey,LongdefaultValue){

returngetProp().getLong(key,defaultValue);

}

publicstaticBooleangetBoolean(Stringkey){

returngetProp().getBoolean(key);

}

publicstaticBooleangetBoolean(Stringkey,BooleandefaultValue){

returngetProp().getBoolean(key,defaultValue);

}

publicstaticbooleancontainsKey(Stringkey){

returngetProp().containsKey(key);

}

}

测试:

assets下有config.properties

内容如下:

login.url=

dev.mode=true

使用获取方式如下:

StringloginUrl=PropKit.use("config.properties").get("login.url");

booleandevMode=PropKit.getBoolean("dev.mode");

从上边可以看出,.use是填写的assets根目录下的properties文件名称,代码中默认的文件名叫做config.properties,所以也可以不用写.use,可以直接进行获取。

0 +1

版权声明

发表评论

请文明留言

发表

共 0 条评论

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。