为什么它不存储所有键值而不是存储最后更新的值?

时间:2014-03-13 09:49:50

标签: java file-io

使用下面的代码我可以存储在属性文件中,但问题是它存储的是最后存储的值。我的意思是,如果我正在调整值val1val2val3 ... val5它只存储var5值而不是其他值...是否有任何值这样做的方法?

    prop = new Properties();

    try {  
           /* //set the properties value  
            prop.setProperty(Key, value);  

            System.out.println("Updating the value for:"+Key);
            //save properties to project testdata.properties file
            prop.store(new FileOutputStream(System.getProperty("user.dir")
            + "xyz.properties"), null);  */

        file = new File(System.getProperty("user.dir")
                + "\\src\\test\\java\\config\\testdata123.properties");
        Properties table = new Properties();

        table.setProperty(Key, value);

        System.out.println("Properties has been set in HashTable:" + table);

        System.out.println("The Key values are :" + value);
        //saving the properties in file
        saveProperties(table);

        System.out.println("After the change in HashTable:" + table);
        //saving the properties in file


    } catch (IOException ex) {
        ex.printStackTrace();
        return "Fail" + "While updating the testdata.properties file";
    }

    return "Pass";
}

public static void saveProperties(Properties p) throws IOException {
    FileOutputStream fr = new FileOutputStream(file);
    p.store(fr, "Properties");
    System.out.println("After saving properties:" + p);
}

static void loadProperties(Properties p) throws IOException {
    FileInputStream fi = new FileInputStream(file);
    p.load(fi);
    fi.close();

    System.out.println("After Loading properties:" + p);
}

完整代码:

   public static String setPropertiesValue(String Key, String value) {

    try {
        file = new File(System.getProperty("user.dir")
                + "\\src\\test\\java\\config\\xyz.properties");
        Properties table = new Properties();

        table.load(new FileInputStream(file));

        table.setProperty(Key, value);

        System.out.println("Properties has been set in HashTable:" + table);

        System.out.println("The Key values are :" + value);
        //saving the properties in file
        saveProperties(table);

        System.out.println("After the change in HashTable:" + table);
        //saving the properties in file


    } catch (IOException ex) {
        ex.printStackTrace();
        return "Fail" + "While updating the testdata.properties file";
    }

    return "Pass";
}

public static void saveProperties(Properties p) throws IOException {
    FileOutputStream fr = new FileOutputStream(file);
    p.store(fr, "Properties");

    System.out.println("After saving properties:" + p);
}

static void loadProperties(Properties p) throws IOException {
    FileInputStream fi = new FileInputStream(file);
    p.load(fi);
    fi.close();

    System.out.println("After Loading properties:" + p);
}
enter code here

由于

1 个答案:

答案 0 :(得分:2)

您正在创建一个名为' table'的新属性。每次将属性保存到文件时覆盖文件。而是从现有文件加载属性并设置新键,值并保存。     希望这应该有用!

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;

    public class Test {
        static File file = null;

        /**
         * @param args
         */
        public static void main(String[] args) {
            setPropertiesValue("name1", "value1");
            setPropertiesValue("name2", "value2");
            setPropertiesValue("name3", "value3");
            setPropertiesValue("name4", "value4");
            setPropertiesValue("name5", "value5");
        }

        public static String setPropertiesValue(String Key, String value) {

            try {
                file = new File(System.getProperty("user.dir") + "\\src\\test\\java\\config\\xyz.properties");

                Properties table = new Properties();

                table.load(new FileInputStream(file));

                table.setProperty(Key, value);

                System.out.println("Properties has been set in HashTable:" + table);

                System.out.println("The Key values are :" + value);
                // saving the properties in file
                saveProperties(table);

                System.out.println("After the change in HashTable:" + table);
                // saving the properties in file

            } catch (IOException ex) {
                ex.printStackTrace();
                return "Fail" + "While updating the testdata.properties file";
            }

            return "Pass";
        }

        public static void saveProperties(Properties p) throws IOException {
            FileOutputStream fr = new FileOutputStream(file);
            p.store(fr, "Properties");

            System.out.println("After saving properties:" + p);
        }

        static void loadProperties(Properties p) throws IOException {
            FileInputStream fi = new FileInputStream(file);
            p.load(fi);
            fi.close();

            System.out.println("After Loading properties:" + p);
        }
    }

属性文件的结果(值)如下

属性

Thu Mar 13 12:18:25 GMT 2014

NAME5 =值5
NAME4 = VALUE4
NAME3 =值3
2 =值2
NAME1 =值1
这是你想要得到的结果吗?或者我错过了什么?