2013-05-29 8 views
6

私は.propertiesファイルのファイルを読み込むと、次のようなコードを持っていることを試みた:プロパティファイルはどこに置くのですか?

public final class Config { 
    static { 
    Properties properties = new Properties(); 
    InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); 

    if (propertiesStream != null) { 
     try { 
     properties.load(propertiesStream); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } else { 
     System.out.println("file not found"); 
    } 
    } 
} 

が、それは、ファイルが見つからないと言っ保持します。

プロパティの内容は

pwd=passw0rd 

誰もがこの問題を解決する方法を知っているのですか?

答えて

15

それはそのMavenプロジェクトがsrc/main/resourcesディレクトリ

0

にそれを置けば、それはWebContent/Web-Inf/フォルダ

になり、あなたのXMLファイルにBeanを定義する必要があり、あなたのルートのソースパッケージにそれを置く、クラスパスにする必要があります

<bean id="propertyConfigurer" class="com.your.project.util.properties.ApplicationProperties"> 
    <property name="locations"> 
     <list> 
      <value>/WEB-INF/application.properties</value> 
     </list> 
    </property> 
</bean> 
+0

使用しているフレームワークが春であれば、これは正常に動作します –

0

config.propertiesをConfig.javaと同じフォルダに保存することもできます。

//InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); 
InputStream propertiesStream = Config.class.getResourceAsStream("config.properties"); 
0

あなたは

のInputStream propertiesStream、パスを選択する二つの選択肢、

  1. オープンファイルを含むフォルダを持っているし、パスを取得など、ファイルの文字列にそのパスを保存することができます= Object.class.getResourceAsStream(path + File.seperator + "config.properties");

  2. SRCパスにファイルを保存し、

    ワークスペース - >プロジェクト名 - > Copyhere

関連する問題