Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a file from server in play framework

I have the following file

/app/menus/menu1.yml

and I'd like to read it's contents

--

short answer:

fileContent = play.vfs.VirtualFile.fromRelativePath("/app/menus/menu1.yml").contentAsString();
like image 637
opensas Avatar asked Dec 23 '10 14:12

opensas


People also ask

Is Play framework backend?

Play comes with two configurable server backends, which handle the low level work of processing HTTP requests and responses to and from TCP/IP packets. Starting in 2.6. x, the default server backend is the Akka HTTP server backend, based on the Akka-HTTP server. Prior to 2.6.

Is Play framework good?

Play Framework is the best full-stack web framework available in the current market to develop Reactive Web Applications, Reactive systems, Reactive architecture, Reactive microservices, or Reactive libraries using both FP and RP paradigms, that is, FRP.

What is activator in play framework?

Play has been updated to use Activator so that we can: Extend the range of templates we provide for getting started with Play projects. Activator supports a much richer library of project templates. Templates can also include tutorials and other resources for getting started.

Is Play framework asynchronous?

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers.


2 Answers

PlayFramework is built using the Java language.

In your code, there is no restriction about the usage of the java API. So, your file can be read using the standard java code, if you know the file absolute path:

java.io.File yourFile = new java.io.File("/path/app/menus/menu1.yml");
java.io.FileReader fr = new java.io.FileReader(yourFile);
// etc.

If you want to access a File in a relative path from your Play application, your can use the play "VirtualFile" class: http://www.playframework.org/documentation/api/1.1/play/vfs/VirtualFile.html

VirtualFile vf = VirtualFile.fromRelativePath("/app/menus/menu1.yml");
File realFile = vf.getRealFile();
FileReader fr = new FileReader(realFile);
// etc.
like image 52
Benoit Courtine Avatar answered Sep 30 '22 16:09

Benoit Courtine


For Play 2.0 in Scala you want to use Play.getFile(relativePath: String)

like image 22
tksfz Avatar answered Sep 30 '22 17:09

tksfz