116

のパッケージ構造に埋め込まれているプロパティ ファイルを読み取る必要がありますcom.al.common.email.templates

私はすべてを試しましたが、それを理解することはできません。

最終的に、私のコードはサーブレット コンテナーで実行されることになりますが、コンテナーには何の依存もしたくありません。私は JUnit テスト ケースを作成しており、両方で動作する必要があります。

4

9 に答える 9

238

パッケージ内のクラスからプロパティをロードするときに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

絶対パス (「/」で始まるパス) を使用すると、現在のパッケージは無視されます。

于 2008-12-02T08:52:20.853 に答える
50

Joachim Sauer の回答に追加するには、静的なコンテキストでこれを行う必要がある場合は、次のようなことができます。

static {
  Properties prop = new Properties();
  InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
  prop.load(in);
  in.close()
}

(以前と同様、例外処理は省略されています。)

于 2009-07-03T03:19:25.283 に答える
12
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();
    }

}
于 2012-02-06T20:53:30.960 に答える
10
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());
        }

    }
}
于 2013-05-01T05:53:29.340 に答える
2

ロードメソッドを介してPropertiesクラスを使用すると仮定すると、入力ストリームを取得するために ClassLoader getResourceAsStreamを使用していると思います。

名前をどのように渡していますか。次の形式にする必要があるようです。/com/al/common/email/templates/foo.properties

于 2008-12-02T08:54:21.223 に答える
1

この呼び出しでこの問題を解決できました

Properties props = PropertiesUtil.loadProperties("whatever.properties");

さらに、whatever.properties ファイルを /src/main/resources に配置する必要があります。

于 2017-01-02T14:30:58.810 に答える
-2

以下のコードを使用してください:

    プロパティ p = new Properties();
    StringBuffer パス = 新しい StringBuffer("com/al/common/email/templates/");
    path.append("foo.properties");
    入力ストリーム 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(); } }
于 2014-02-03T17:49:53.547 に答える