Android 文件方式读取与修改配置(properties)方法

xiaoxiao2021-02-28  44

//读取 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) { } }

}

转载请注明原文地址: https://www.6miu.com/read-2625910.html

最新回复(0)