android-app icon indicating copy to clipboard operation
android-app copied to clipboard

读取属性文件效率质疑?

Open YorkTsai2011 opened this issue 12 years ago • 0 comments

在AppContext类中 /** * 获取登录信息 * @return */

public User getLoginInfo() {        
    User lu = new User();       
    lu.setUid(StringUtils.toInt(getProperty("user.uid"), 0));
    lu.setName(getProperty("user.name"));
    lu.setFace(getProperty("user.face"));
    /* ohter code */
    return lu;
}

追踪getProperty()方法的定义,可以找到相关两个方法,如下:

    public String get(String key) {
    Properties props = get();
    return (props != null) ? props.getProperty(key) : null;
}

public Properties get() {
    FileInputStream fis = null;
    Properties props = new Properties();
    try {
        File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
        fis = new FileInputStream(dirConf.getPath() + File.separator + APP_CONFIG);
        props.load(fis);
    } 
            /* other code */
    return props;
}

不难发现,每次获取属性集中的某个属性,都要通过打开一次属性文件,读取props,即将所有属性都读出来。换句话说,如果要获取10个属性,这里就要打开10次属性文件,试问这样的IO效率是否他低了,有没有更好的解决办法?

YorkTsai2011 avatar Dec 20 '12 11:12 YorkTsai2011