Liny_@NotePad

沉迷ACG中

读写properties

YOYO posted @ 2009年5月26日 17:51 in 【Java SE】 with tags Properties , 2011 阅读

新建一个java.util.Properties对象p,

第一步加载properties:

try {
        p.load(new FileInputStream(new File(CONFIG_PATH)));
} catch (IOException e) {
        e.printStackTrace();
}

读取操作很简单,直接调用它的getProperty方法:

p.getProperty("admin_password")

写操作需要一个FileWriter:

try {
        FileOutputStream out = new FileOutputStream(new File(CONFIG_PATH));
        p.setProperty(key, value);
        p.store(out, key);
        out.close();
} catch (IOException e) {
        e.printStackTrace();
}

p.setProperty方法设置key键的属性值为value,store方法存储。

每次都必须new FileOutputStream,因为文件会被清空 = = 不知有什么比较简单的方法。

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter