Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java的play框架中使用属性文件?_Java_Playframework_Playframework 2.0 - Fatal编程技术网

如何在java的play框架中使用属性文件?

如何在java的play框架中使用属性文件?,java,playframework,playframework-2.0,Java,Playframework,Playframework 2.0,我是个新手。我想知道如何在play框架中使用属性文件 我的财产档案是 conf/test.properties name=kumar framework=playframework 所以现在我想在控制器类(Application.java)中使用test.properties 请告诉我需要执行哪些步骤。放置在conf/文件夹中的所有文件都会自动添加到您的类路径中。您应该能够通过以下方式访问您的文件: Properties prop; try { prop = new Properties(

我是个新手。我想知道如何在play框架中使用属性文件

我的财产档案是

conf/test.properties
name=kumar
framework=playframework
所以现在我想在控制器类(Application.java)中使用test.properties


请告诉我需要执行哪些步骤。

放置在
conf/
文件夹中的所有文件都会自动添加到您的类路径中。您应该能够通过以下方式访问您的文件:

Properties prop;
try {
  prop = new Properties();
  InputStream is = this.getClass().getResourceAsStream("test.properties");
  prop.load(is);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

String name = prop.getProperty("name");
String framework = prop.getProperty("playframework");
注意:我没有测试上述任何一项

更新:

我刚刚意识到这个问题与我的问题几乎是重复的,但因为我的解决方案还包括如何访问文件中的属性,所以我将保持原样

此外,由于您的控制器方法很可能是静态的,因此上述方法可能无法编译。在我提到的答案中,他们建议使用Play提供的设施!:

Play.application().resourceAsStream("test.properties")

您可以通过以下操作访问play framework中的属性文件:

val ConfigLoader = play.Play.application.configuration
  implicit val connector = ContactPoints(Seq(ConfigLoader.getString("cassandra.host"))).keySpace(ConfigLoader.getString("cassandra.keyspace"))

Play.application已被弃用。抱歉,在您的示例中,不清楚如何提到文件名。OP有一个名为test.properties的文件。在您的示例中,您在哪里使用它?