how to read a file from server in play framework

前端 未结 4 2067
悲哀的现实
悲哀的现实 2021-02-07 13:16

I have the following file

/app/menus/menu1.yml

and I\'d like to read it\'s contents

--

short answer:

fileConten         


        
4条回答
  •  孤城傲影
    2021-02-07 13:55

    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.
    

提交回复
热议问题