2012-12-21 6 views
14

내 응용 프로그램에 속성 파일을 추가해야합니다. 이 파일을 controller 디렉토리에 추가했지만로드 할 수 없습니다 (클래스 경로에 no?) - 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 " –

관련 문제