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

Lah*_*iep 0 java io runtime

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

Properties p = new Properties();
p.load(fileinpustream ...);
...
p.setProperty("NewKey","NewValue");
p.store(outputstream, "comment");
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

小智 5

确保您的"NewValue"不为

这是来自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();
}
Run Code Online (Sandbox Code Playgroud)

...