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.
-
1Nothing Play-specific about it. Please refer to this question: http://stackoverflow.com/questions/1284423/read-entire-file-in-scala – Carsten Feb 20 '14 at 20:35
-
@Carsten, change comment to answer pls :) – biesior Feb 20 '14 at 21:41
3 Answers
There is no real designated location where data files should go. I usually set a path in my application.conf
and then read it in the application via
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.application().path()
which returns a 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:
val lines = scala.io.Source.fromFile("file.txt").mkString
-
how do I use a 3rd party library that stores config in, say a `properties/` directory somewhere? I can modify the 3rd party library but not with any Play specific methods (i.e., pure Scala code only). – Jus12 Sep 01 '14 at 06:54
-
@Jus12 I don't really understand what you want. You should probably ask this as a separate question (and elaborate a bit). – Carsten Sep 01 '14 at 07:33
-
Basically what I am asking is that the 3rd party app keeps the files in specific folder and cannot import anything from `Play._`. How can it still read its files when used within play as an external dependency? – Jus12 Sep 01 '14 at 07:51
You can place any resource file in the folder /conf and load it (Programatically) as explained here: Custom configuration files - Play! Framework 2.0

- 1
- 1

- 2,046
- 19
- 27
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.
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
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
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.

- 1
- 1

- 11,964
- 10
- 61
- 87