1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

複数のプロパティファイルの情報をMap<String, Map<String, String>>の変数に格納

Posted at

■test.properties

test.properties
#\u3053\u306E\u30B5\u30A4\u30C8\u304B\u3089\u2192http://otndnld.oracle.co.jp/products/jdev/htdocs/1013/jdev_oc4jdp4/jdev_oc4jdp4_buildsample.html
# JDeveloper\u3092\u30A4\u30F3\u30B9\u30C8\u30FC\u30EB\u3057\u305F\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA
jdev.home=D:/JDeveloper1
# OC4J\u3092\u30A4\u30F3\u30B9\u30C8\u30FC\u30EB\u3057\u305F\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA
oracle.home=D:/oc4j1
# Model\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u306E\u8A2D\u5B9A
model.dir=../Model1
model.src.dir=${model.dir}/src1
model.output.dir=${model.dir}/classes1

■test2.properties

test2.properties
#\u3053\u306E\u30B5\u30A4\u30C8\u304B\u3089\u2192http://otndnld.oracle.co.jp/products/jdev/htdocs/1013/jdev_oc4jdp4/jdev_oc4jdp4_buildsample.html
# JDeveloper\u3092\u30A4\u30F3\u30B9\u30C8\u30FC\u30EB\u3057\u305F\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA
jdev.home=D:/JDeveloper2
# OC4J\u3092\u30A4\u30F3\u30B9\u30C8\u30FC\u30EB\u3057\u305F\u30C7\u30A3\u30EC\u30AF\u30C8\u30EA
oracle.home=D:/oc4j2
# Model\u30D7\u30ED\u30B8\u30A7\u30AF\u30C8\u306E\u8A2D\u5B9A
model.dir=../Model2
model.src.dir=${model.dir}/src2
model.output.dir=${model.dir}/classes2

■コード

MergeProperties.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class MergeProperties {
    public static void main(String[] args) {
        // 結果を入れるためのMap
        Map<String, Map<String, String>> mergedProperties = new HashMap<>();
        // プロパティファイルを開く
        String[] propertyFiles = {"test.properties", "test2.properties"};
        for (String file : propertyFiles) {
            try (InputStream input = new FileInputStream(file)) {
                Properties properties = new Properties();
                properties.load(input);
                // ファイル名からキーを抽出
                String key = file.substring(0, file.lastIndexOf('.'));
                // 内側のMapを作成してプロパティを追加
                Map<String, String> innerMap = new HashMap<>();
                for (String propName : properties.stringPropertyNames()) {
                    innerMap.put(propName, properties.getProperty(propName));
                }
                // 外側のMapに内側のMapを追加
                mergedProperties.put(key, innerMap);
            } catch (IOException e) {
                e.printStackTrace();
                // ファイル読み込み中にエラーが発生した場合の処理
            }
        }
        // 結果を出力
        for (Map.Entry<String, Map<String, String>> entry : mergedProperties.entrySet()) {
            System.out.println("File: " + entry.getKey());
            Map<String, String> innerMap = entry.getValue();
            for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
                System.out.println(innerEntry.getKey() + " : " + innerEntry.getValue());
            }
            System.out.println();
        }
    }
}

■出力

File: test2
jdev.home : D:/JDeveloper2
model.output.dir : ${model.dir}/classes2
oracle.home : D:/oc4j2
model.src.dir : ${model.dir}/src2
model.dir : ../Model2

File: test
jdev.home : D:/JDeveloper1
model.output.dir : ${model.dir}/classes1
oracle.home : D:/oc4j1
model.src.dir : ${model.dir}/src1
model.dir : ../Model1
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?