2016-08-14 121 views
0

如果它不存在,我試圖向config.properties添加一個新屬性。有沒有辦法做到這一點?如果爲null,則將屬性添加到屬性文件

我目前的配置類看起來是這樣的:

package com.template; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.Properties; 

public class Config { 

    static File folder = new File(System.getProperty("user.home") + "/Documents/Electrocode Productions/template"); 

    public static void add() { 
     if(!folder.exists()) { 
      try { 
       folder.mkdirs(); 
      } catch(SecurityException e) { 
       Log.error(e); 
      } 
     } 

     Properties config = new Properties(); 
     OutputStream output = null; 
     Path filePath = Paths.get(folder + "/config.properties");  
     if(!(filePath == null)) { 
      try { 
       output = new FileOutputStream(folder + "/config.properties"); 

       config.setProperty("log", "true"); 

       config.store(output, null); 
      } catch(IOException e) { 
       Log.error(e); 
      } finally { 
       if(output !=null) { 
        try { 
         output.close(); 
        } catch(IOException e) { 
         Log.error(e); 
        } 
       } 
      } 
     } 
    } 

    public static String get(String value) { 
     Properties config = new Properties(); 
     InputStream input = null; 

     try { 
      input = new FileInputStream(folder + "/config.properties"); 

      config.load(input); 
     } catch(IOException e) { 
      Log.error(e); 
     } finally { 
      if (input != null) { 
       try { 
        input.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return config.getProperty(value).trim(); 
    } 
} 

這是工作,因爲它不會覆蓋該文件,如果您編輯它,但如果你刪除的條目,你需要徹底刪除整個文件重新添加該條目。

我的最終目標是能夠關閉程序,編輯配置文件,然後用新參數重新打開配置文件,但是如果刪除參數,它不會因程序而崩潰,因爲它依賴於從配置文件的答案。 (我希望這是有道理的,它基本上像大多數電子遊戲)。

+0

這不是非常清楚。你能構建一個[最小測試用例](http://stackoverflow.com/help/mcve)來說明嗎? –

+0

@Oliver發現問題只是我無法「返回null」我不得不改變它以「不知道」返回「;」然後使用get(value).equals(「unknow」)而不是.equals(null)。謝謝你試圖幫助我! – BudSkywalker

回答

0

在使用它之前,您需要驗證從屬性中獲得的值。例如你不能.trim()這個值不在這裏。

public class Config { 

    static final File folder = new File(System.getProperty("user.home") + "/Documents/Electrocode Productions/template"); 
    static final File file = new File(folder, "config.properties"); 

    public static void add() throws IOException { 
     if (file.exists()) 
      return; 

     // create directories as needed. 
     folder.mkdirs(); 

     Properties config = new Properties(); 
     config.setProperty("log", "true"); 

     try (OutputStream out = new FileOutputStream(file)) { 
      config.store(out, null); 
     } 
    } 

    public static String get(String key, String defaultValue) { 
     if (!file.exists()) 
      return defaultValue; 

     try (InputStream in = new FileInputStream(file)) { 
      Properties config = new Properties(); 

      config.load(input); 
     } catch(IOException e) { 
      Log.error(e); 
      return defaultValue; 
     } 

     String value = config.getProperty(key); 
     if (value == null) 
      return defaultValue; 
     value = value.trim(); 
     if (value.isEmpty()) 
      return defaultValue; 
     return value; 
    } 
} 
+0

非常感謝你的代碼!我發現問題只是我無法「返回null」。我不得不改變它以「不知道」返回「;」然後使用get(value).equals(「unknow」)而不是.equals(null)。再次感謝您的幫助。 – BudSkywalker

+0

@BudSkywalker這是使用默認值最好的地方。如果未知,您可以傳入想要的值。順便說一句,你可以使用null,但比較必須是'if(value == null)' –