2012-12-21 119 views
14

我需要添加一些屬性文件到我的應用程序。我已將此文件添加到controller目錄,但無法加載它們(在類路徑中沒有?) - InputStream爲空。在哪裏可以訪問這個文件?去哪裏投入資源?

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「' –