2016-08-14 2 views
0

config.propertiesに新しいプロパティを追加しようとしています。これを行う方法はありますか?nullの場合はプロパティファイルにプロパティを追加します。

私の現在のconfigクラスは次のようになります。

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を返すことができなかった"ことを発見しました。私はそれを "知らない" "返す"ように変更しなければならなかった; .equals(null)の代わりにget(value).equals( "unknow")を使用します。私を助けてくれてありがとう! – 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を返すことはできませんでした"ことを発見した私はそれを "知らない" "返す"ように変更しなければならなかった; .equals(null)の代わりにget(value).equals( "unknow")を使用します。もう一度あなたのお手伝いをしてください。 – BudSkywalker

+0

@BudSkywalkerこれは、デフォルト値を使用するのが最適です。それがわからない場合は、あなたが望む価値を渡すことができます。 Btwではnullを使用できますが、比較は 'if(value == null)'でなければなりません –

関連する問題