Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file in Play Framework 2.2.1?

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.

like image 540
randombits Avatar asked Feb 20 '14 20:02

randombits


People also ask

How do I run a play Framework project?

Generate configuration To debug, start your application with sbt -jvm-debug 9999 run and in Eclipse right-click on the project and select Debug As, Debug Configurations. In the Debug Configurations dialog, right-click on Remote Java Application and select New. Change Port to 9999 and click Apply.

Is Play framework still used?

Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.

How do I run play framework in Windows?

To run the Play framework, you need JDK 6 or later. If you are using MacOS, Java is built-in. If you are using Linux, make sure to use either the Sun JDK or OpenJDK (and not gcj, which is the default Java command on many Linux distros). If you are using Windows, just download and install the latest JDK package.

What is activator in play framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.


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
like image 77
Carsten Avatar answered Nov 01 '22 11:11

Carsten


You can place any resource file in the folder /conf and load it (Programatically) as explained here: Custom configuration files - Play! Framework 2.0

like image 22
Didac Montero Avatar answered Nov 01 '22 09:11

Didac Montero


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.

like image 1
Supun Wijerathne Avatar answered Nov 01 '22 09:11

Supun Wijerathne