2012-12-21 19 views
14

アプリケーションにいくつかのプロパティファイルを追加する必要があります。このファイルをcontrollerディレクトリに追加しましたが、ロードできません(クラスパスには何もありません)。InputStreamはnullです。どこにこのファイルを置くことができますか?リソースを配置する場所

public class Application extends Controller { 

    static { 
     try { 
      Properties p = new Properties(); 
      InputStream in = Application.class.getClassLoader().getResourceAsStream("accounts.properties"); 
      if(in != null) { 
       p.load(in); 
       in.close(); 
      } else { 
       error("null inputstream"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }  
    } 

    // Actions below 
    // ... 
} 

答えて

23

あなたはPlayアプリのconfフォルダに入れなければなりません。

confディレクトリのサブフォルダを使用することもできます。例えば

conf/foo/bar.txt 

を使用してアクセスすることができます。

InputStream in = MyClass.class. getResourceAsStream("/foo/bar.txt") 

あなたはまた、あなたのproject/Build.scalaファイルを更新して追加することで、あなたのアプリでカスタムリソースのディレクトリを追加することができます。

val main = play.Project(appName, appVersion, appDependencies).settings(
     ... 
     resourceDirectory in Compile <<= baseDirectory/"myresources" 
     ... 
) 
+5

新しいバージョンの 'sbt'では' resourceDirectories in Compile + = baseDirectory.value/"と書かれます。 myresources " –

関連する問題