Java - 在运行时向属性文件添加新条目对

时间:2012-01-09 11:18:24

标签: java io runtime

我想在运行时向配置属性文件中添加一个新的NewKey-NewValue对。我试过了:

Properties p = new Properties();
p.load(fileinpustream ...);
...
p.setProperty("NewKey","NewValue");
p.store(outputstream, "comment");

但是我总是在setProperty行上得到一个NullPointerException。 有什么建议吗?

感谢。

1 个答案:

答案 0 :(得分:5)

确保您的“NewValue”不是 null

这是来自Hashtable,java.util.Properties的父级

...

public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
    throw new NullPointerException();
}

...