2016-03-24 19 views
0

私はそれぞれ異なる目的のために約10個のプロパティファイルを持っており、それぞれのファイルの新しいクラスを作成してその中のコンテンツにアクセスします。私は、.getValue(key,propertFileName)のようなメソッドでクラスを作成する可能性があると考えています。Javaで単一のマネージャクラスを持つ複数のプロパティファイルにアクセスする方法

私は.getValue("name.type","sample.properties");以下

のように呼び出した場合、私はプロパティファイルから値を取得するために使用していると私は、コアJavaを使用していたコードであるように。

public class PropertiesCache { 

    private final Properties configProp = new Properties(); 

    private PropertiesCache() { 
     InputStream in = this.getClass().getClassLoader().getResourceAsStream("sample.properties"); 
     System.out.println("Read all properties from file"); 
     try { 
      configProp.load(in); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private static class LazyHolder { 
     private static final PropertiesCache INSTANCE = new PropertiesCache(); 
    } 

    public static PropertiesCache getInstance() { 
     return LazyHolder.INSTANCE; 
    } 

    public String getProperty(String key) { 
     return configProp.getProperty(key); 
    } 

    public Set<String> getAllPropertyNames() { 
     return configProp.stringPropertyNames(); 
    } 

    public boolean containsKey(String key) { 
     return configProp.containsKey(key); 
    } 
} 

以下の呼び出しで、私はsample.propertiesファイルから値を取得できます。

PropertiesCache.getInstance().getProperty("name.type"); 

上記のコードでは、1つのプロパティファイルからコンテンツを取得できます。複数のファイルから値を取得するには、ファイルごとに別々のCacheクラスを作成する必要があります。

あなたは、単一のマネージャクラスを持つ異なるプロパティファイルからプロパティ値を取得する方法を教えてください。

したがって.getValue("name.type","sample.properties")PropertiesCache.getInstance().getProperty("name.type")のように同じ値を返す必要があります。

答えて

1

さまざまなプロパティオブジェクトをマップに配置します。マップには、ファイル名をキーとして読み込み、読み込まれたプロパティバンドルは値として読み込まれます。

の線に沿ってだろう "GET":必要に応じてエラー処理を追加...

Map<String, Properties> propsCache = .... // declare and fill elsewhere 

public String getProperty(String name, String file) { 
    Properties p = propsCache.get(file); 
    return p.getProperty(name); 
} 

0

すべてのプロパティファイルをMapにロードし、ファイル名をキーとして使用します。たとえば:

public class PropertiesCache 
{ 

    private Map<String, Properties> map = new HashMap<>(); 

    public PropertiesCache(List<String> filenames) 
    { 

     for (String f : filenames) 
     { 
      Properties props = new Properties(); 
      try 
      { 
       props.load(new FileReader(f)); 
       map.put(f, props); 
      } 
      catch (IOException ex) 
      { 
       // handle error 
      } 
     } 

    } 

    public String getProperty(String file, String key) { 

     Properties props = map.get(file); 

     if (props != null) { 
      return props.getProperty(key); 
     } 

     return null; 
    } 

    public Set<String> getAllPropertyNames() 
    { 

     Set<String> values = new HashSet<>(); 

     for (Properties p : map.values()) 
     { 
      values.addAll(p.stringPropertyNames()); 
     } 

     return values; 

    } 

    public boolean containsKey(String key) 
    { 
     for (Properties p : map.values()) 
     { 
      if (map.containsKey(key)) 
      { 
       return true; 
      } 
     } 

     return false; 
    } 
} 

あなたはファイル名を受け入れるようにメソッドのシグネチャを変更する場合があります

public Set<String> getAllPropertyNames(String file) 
{ 

    Properties props = map.get(file); 

    if (props != null) 
    { 
     return props.stringPropertyNames(); 
    } 

    return null; 

} 

public boolean containsKey(String file, String key) 
{ 
    Properties props = map.get(file); 

    if (props != null) 
    { 
     return props.contains(key); 
    } 

    return false; 
} 
+0

おかげ@starf。それは私のために働いた。コードの変更のみが必要です。マップオブジェクトを初期化する必要があります。すべてがうまくいった。もう一度ありがとう – Krishna

+0

@KrishnaD素晴らしい!私はあなたの変更で答えを編集します。私は本当にそれをテストしなければなりません... – starf

関連する問題