//读取
private String getValue(String key,String defValue){
  File config = new File(m_sConfigFile);
    if (!config.exists()) {
        try{
            config.createNewFile();  //不存在则创建
        }catch (Exception e){
       }
    }
    
    Properties props = new Properties();
    try{
        FileInputStream is = new FileInputStream(m_sConfigFile);
        props.load(is);
    }catch (Exception e){
        }

    try {
        String sValue = null;
        sValue = props.getProperty(key);
        if(sValue == null){
            setValue(key,defValue);
            return defValue;
        }
        return sValue;

    }catch (Exception e){
      }

    return defValue;
}

//修改
private String setValue(String key,String value) {

    File config = new File(m_sConfigFile);
    if (!config.exists()) {
        try{
            config.createNewFile();
        }catch (Exception e){
       }
    }
     Properties props = new Properties();
    try{
        FileInputStream is = new FileInputStream(m_sConfigFile);
        props.load(is);
    }catch (Exception e){
    }
    props.setProperty(key, value);

    try {
        FileOutputStream out = new FileOutputStream(m_sConfigFile);
        props.store(out, null);
    } catch (Exception e) {
    }
}

}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐