Javaパッケージからプロパティファイルを読み込む
-
22-07-2019 - |
質問
com.al.common.email.templates
のパッケージ構造に埋め込まれているプロパティファイルを読み取る必要があります。
すべてを試しましたが、理解できません。
最終的に、私のコードはサーブレットコンテナで実行されますが、コンテナに依存することはしたくありません。 JUnitテストケースを作成し、両方で動作する必要があります。
解決
パッケージ com.al.common.email.templates
のクラスからプロパティをロードするときに使用できます
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
(必要な例外処理をすべて追加します)。
クラスがそのパッケージにない場合、InputStreamをわずかに異なる方法で取得する必要があります:
InputStream in =
getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
getResource()
/ getResourceAsStream()
の相対パス(先頭に「/」が付いていないパス)は、リソースを表すディレクトリに対してリソースが検索されることを意味しますクラスが含まれるパッケージ。
java.lang.String.class.getResource(" foo.txt")
を使用すると、(不適切な)ファイル /java/lang/String/foo.txt <が検索されます/ code>をクラスパスに。
絶対パス(「/」で始まるパス)を使用すると、現在のパッケージが無視されます。
他のヒント
Joachim Sauerの答えに追加するには、静的なコンテキストでこれを行う必要がある場合、次のようなことができます:
static {
Properties prop = new Properties();
InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
prop.load(in);
in.close()
}
(例外処理は以前と同様に省略されています。)
次の2つのケースは、 TestLoadProperties
という名前のサンプルクラスからのプロパティファイルの読み込みに関連しています。
ケース1: ClassLoader
InputStream inputStream = TestLoadProperties.class.getClassLoader()
.getResourceAsStream("A.config");
properties.load(inputStream);
この場合、正常にロードするには、プロパティファイルが root / src
ディレクトリにある必要があります。
ケース2: ClassLoader
を使用せずにプロパティファイルをロードする
InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
この場合、正常にロードするには、プロパティファイルは TestLoadProperties.class
ファイルと同じディレクトリにある必要があります。
注: TestLoadProperties.java
と TestLoadProperties.class
は2つの異なるファイルです。前者の .java
ファイルは通常プロジェクトの src /
ディレクトリにあり、後者の .class
ファイルは通常次の場所にあります。 bin /
ディレクトリ。
public class Test{
static {
loadProperties();
}
static Properties prop;
private static void loadProperties() {
prop = new Properties();
InputStream in = Test.class
.getResourceAsStream("test.properties");
try {
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public class ReadPropertyDemo {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
"com/technicalkeeda/demo/application.properties"));
System.out.println("Domain :- " + properties.getProperty("domain"));
System.out.println("Website Age :- "
+ properties.getProperty("website_age"));
System.out.println("Founder :- " + properties.getProperty("founder"));
// Display all the values in the form of key value
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println("Key:- " + key + "Value:- " + value);
}
} catch (IOException e) {
System.out.println("Exception Occurred" + e.getMessage());
}
}
}
プロパティを使用すると仮定しますクラス、 load メソッド。ClassLoader getResourceAsStream を使用して、入力ストリームを取得します。
名前をどのように渡しているのかは、 /com/al/common/email/templates/foo.properties
この呼び出しでこの問題を解決できました
Properties props = PropertiesUtil.loadProperties("whatever.properties");
さらに、whatever.propertiesファイルを/ src / main / resourcesに配置する必要があります
クラスのパッケージを処理する必要のない、上記よりもさらにシンプルなソリューションについて言及している人はいません。 myfile.propertiesがクラスパスにあると仮定します。
Properties properties = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
properties.load(in);
in.close();
楽しむ
以下のコードを使用してください:
Properties p = new Properties();
StringBuffer path = new StringBuffer("com/al/common/email/templates/");
path.append("foo.properties");
InputStream fs = getClass().getClassLoader()
.getResourceAsStream(path.toString());
if(fs == null){
System.err.println("Unable to load the properties file");
}
else{
try{
p.load(fs);
}
catch (IOException e) {
e.printStackTrace();
}
}