属性文件Java

属性文件Java,java,properties,Java,Properties,我想使用一个预先配置的属性文件,加载它,然后在默认的config.properties中已经存在的行下添加一行 我有一个read();字符串值=“ho” 但是当我启动read()和write()以及查看新的config.properties时,我只看到 hey=ho 在我的默认config.properties中 ha=hi hu=hu 但我想在我的新配置中: ha=hi hu=hu hey=ho 我的代码: Properties prop = new Properties

我想使用一个预先配置的属性
文件
,加载它,然后在默认的
config.properties
中已经存在的行下添加一行

我有一个
read();字符串值=“ho”

但是当我启动
read()
write()
以及查看新的
config.properties
时,我只看到

hey=ho
在我的默认config.properties中

 ha=hi 

 hu=hu
但我想在我的新配置中:

ha=hi  

hu=hu

hey=ho
我的代码:

Properties prop = new Properties();
public static PropertiesIParse instance;

public PropertiesIParse() {
    instance = this;
}

public PropertiesIParse getInstance() {
    return instance;
}

public Properties getProp() {
    return prop;
}

public void read() {

    InputStream input = null;

    try {
        String filename = "/config.properties";
        input = PropertiesIParse.class.getResourceAsStream(filename);
        if (input == null) {
            System.out.println("Sorry, unable to find " + filename);
            return;
        }

        getProp().load(input);
        Enumeration<?> e = getProp().propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = getProp().getProperty(key);
            System.out.println("KeyKey : " + key + ", Value : " + value);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public void write() {

    String filename = "config.properties";
    FileOutputStream out;
    //prop = new Properties();

    String key = "hey";
    String value = "ho";

    try {

        out = new FileOutputStream(filename, true);
        getProp().setProperty(key, value);
        getProp().store(out, "--type--"); // <---- variable pour dire YYYY MM DD etc.
        out.close();

    } catch (IOException i) {
        System.out.println("Probleme avec l'écriture dans le fichier Property." + i.getMessage());
        i.printStackTrace();
    }
}
编辑 我改变了你说的,但我得到了同样的东西。。。一个新的config.properties,其中只有我的prop.store(key,value)


我只得到了ok=iiiii。。。我肯定错过了一些东西,感谢您的帮助

当您使用公共构造函数时,您的单例已损坏。每次你打电话-

new PropertiesIParse()
将创建一个新的属性实例,该实例将为空。 将构造函数设为私有,并更改
getInstance()
,如下所示

public PropertiesIParse getInstance() {
    if (instance== null) {
        instance = new PropertiesIParse();
    }
    return instance;
}
然后在不使用新的
的情况下使用它:

PropertiesIParse.getInstance().read();
PropertiesIParse.getInstance().write();

或者从一个不懒惰的单身汉开始。更少的麻烦。在编辑中,您还添加了
Properties=newproperties()。因此,在添加新值后,道具将始终为空。删除这些行,它应该可以工作。我在方法中删除了这两行,但我仍然只在新的config.properties中获得了最后一个条目您确定也从read()方法中删除了它吗?我尝试了你的代码(删除了上面提到的行),效果很好。您是否有其他未在此处显示的可能影响行为的代码?我检查了它,但我认为我完全按照您的顺序操作
new PropertiesIParse()
public PropertiesIParse getInstance() {
    if (instance== null) {
        instance = new PropertiesIParse();
    }
    return instance;
}
PropertiesIParse.getInstance().read();
PropertiesIParse.getInstance().write();