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

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

Android

    为了方便使用,首先创建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<String, Prop> map = new ConcurrentHashMap<String, Prop>();
    
        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

版权声明

 源码  android

 请文明留言

0 条评论