scala 如何在 Play Framework 2.2.1 中读取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21918786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a file in Play Framework 2.2.1?
提问by randombits
I have a static file that I want to read in one of my Play Framework models. The file contains some simple text in it. I can't find any examples or API that shows where the appropriate location is to store such a resource and second, how to access that resource. For whatever it is worth I'm using Play for Scala, but I don't think that's relevant here.
我有一个静态文件,我想在我的 Play Framework 模型之一中读取它。该文件中包含一些简单的文本。我找不到任何示例或 API 来显示存储此类资源的适当位置,其次,如何访问该资源。无论如何,我都在为 Scala 使用 Play,但我认为这与这里无关。
采纳答案by Didac Montero
You can place any resource file in the folder /conf and load it (Programatically) as explained here: Custom configuration files - Play! Framework 2.0
您可以将任何资源文件放在文件夹 /conf 中并按照此处的说明(以编程方式)加载它:自定义配置文件 - 播放!框架 2.0
回答by Carsten
There is no real designated location where data files should go. I usually set a path in my application.confand then read it in the application via
没有真正指定的数据文件应该去的位置。我通常在我的路径中设置一个路径application.conf,然后通过以下方式在应用程序中读取它
Play.application().configuration.getString("my.data.path")
If you want to store it somewhere inside your Play application's directory, you can get its root path via
如果要将其存储在 Play 应用程序目录中的某个位置,可以通过以下方式获取其根路径
Play.application().path()
which returns a java.io.File.
返回一个java.io.File.
For reading files, there is no Play-specific technique. This question has been asked and answered before. In short, to read a small text file, just do this:
对于读取文件,没有特定于 Play 的技术。这个问题以前有人问过,也有人回答过。简而言之,要读取一个小文本文件,只需执行以下操作:
val lines = scala.io.Source.fromFile("file.txt").mkString
回答by Supun Wijerathne
I have answered to a similar question at https://stackoverflow.com/a/37180103/5715934. I think the same answer will be applied for you too.
我在https://stackoverflow.com/a/37180103/5715934回答了一个类似的问题。我认为同样的答案也适用于你。
You can choose a location you prefer other than in dedicated folders for some specific tasks. For an example you can create /resources folder. Don't make any resource folder inside /app folder since it is only a location to store code. Same goes with other specific folders. Then you can use
对于某些特定任务,您可以选择一个您喜欢的位置,而不是在专用文件夹中。例如,您可以创建 /resources 文件夹。不要在 /app 文件夹中创建任何资源文件夹,因为它只是一个存储代码的位置。其他特定文件夹也是如此。然后你可以使用
import import play.Play;
Play.application().getFile("relative_path_from_<Project_Root>);
to access the file inside your code.
访问代码中的文件。
Only this will work perfectly on dev environment. But once you put this in production using the dist file it will not work since the entire resources folder you put will not be added to the dist. In order to do that, you have to explicitly ask play to add the /resources folder to your dist also. For that what you have to do is go to your /build.sbt and add these lines
只有这样才能在开发环境中完美运行。但是,一旦您使用 dist 文件将其投入生产,它将无法工作,因为您放置的整个资源文件夹都不会添加到 dist 中。为了做到这一点,你必须明确要求 play 将 /resources 文件夹也添加到你的 dist 中。为此,您需要做的是转到您的 /build.sbt 并添加这些行
import com.typesafe.sbt.packager.MappingsHelper._
mappings in Universal ++= directory(baseDirectory.value / "resources")
Now if you take and check your dist, you can see it has an additional folder 'resources' inside the dist. Then it will work for the production environment also.
现在,如果你检查你的 dist,你会看到它在 dist 中有一个额外的文件夹“资源”。然后它也适用于生产环境。

