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

本文介绍了如何在Android应用中使用Prop类封装Assets文件的Properties读取,包括创建BaseApplication和Prop工具类,以及PropKit提供的一站式获取方法,便于管理和配置文件操作。
摘要由CSDN通过智能技术生成

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

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

0f9e8264915948f7a1174b4a48c40c63.gif

为了方便使用,首先创建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,可以直接进行获取。

af499b9437efec8e1b25c2bb396e60d7.png

0 +1

版权声明

发表评论

请文明留言

发表

共 0 条评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值