2014-03-13 34 views
0

使用下面的代碼我可以存儲在屬性文件中,但問題是存儲最後存儲的值。我的意思是,如果一個價值val1,val2,val3 ... 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 

感謝

回答

2

要創建一個名爲「表」,並覆蓋每次保存屬性到文件時您的文件中的新屬性。而是從現有文件加載屬性並設置新的密鑰,值並保存它。 希望這應該工作!屬性文件的

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); 
     } 
    } 

結果(值)是如下

屬性

週四年03月13 12點18分25秒GMT 2014

NAME5 =值5
NAME4 = VALUE4
name3 = value3
name2 = value2
name1 = value1
這就是你想要得到的結果嗎?或者我錯過了什麼?

+0

我沒有得到你......你可以在這裏更新我的代碼,以便我能理解嗎? – user3411418

+0

添加此行table.load(new FileInputStream(file));在創建名爲'table'的屬性的行下面。 – rsudha

+0

這是不工作:( – user3411418

相關問題