Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

读取属性文件效率质疑? #17

Open
YorkTsai2011 opened this issue Dec 20, 2012 · 0 comments
Open

读取属性文件效率质疑? #17

YorkTsai2011 opened this issue Dec 20, 2012 · 0 comments

Comments

@YorkTsai2011
Copy link

在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效率是否他低了,有没有更好的解决办法?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant