Java 在play framework 2.0中,将服务器上的文本文件作为字符串读取的最简单方法是什么

Java 在play framework 2.0中,将服务器上的文本文件作为字符串读取的最简单方法是什么,java,playframework-2.0,Java,Playframework 2.0,在Play2.0.4中,我找不到VFS或任何其他简单直接的方法将文本文件作为字符串读取 编辑:我正在寻找一个java解决方案。很抱歉错过了这段信息。Play 2默认情况下导入Google Commons库,使用它和Java的文件,您可以读取任何文件并将其流传递到任何地方。不再需要VFS…Play 2默认情况下导入Google Commons库,使用它和Java的文件,您可以读取任何文件并将其流传递到任何地方。不再需要VFS…您可以使用以下代码: String content = Files.to

在Play2.0.4中,我找不到VFS或任何其他简单直接的方法将文本文件作为字符串读取


编辑:我正在寻找一个java解决方案。很抱歉错过了这段信息。

Play 2默认情况下导入Google Commons库,使用它和Java的
文件
,您可以读取任何文件并将其流传递到任何地方。不再需要VFS…

Play 2默认情况下导入Google Commons库,使用它和Java的
文件
,您可以读取任何文件并将其流传递到任何地方。不再需要VFS…

您可以使用以下代码:

String content = Files.toString(new File("files/config.txt"), Charsets.UTF_8);
它正在使用Google Commons,默认情况下包含在Play中。

您可以使用以下代码:

String content = Files.toString(new File("files/config.txt"), Charsets.UTF_8);

它正在使用默认情况下包含在游戏中的Google Commons。

您是在寻找Java还是Scala解决方案

在Scala中,您可以通过这种方式(无需外部依赖项)读取文件


scala.io.Source.fromFile(“/path/to/file.txt”).getLines

或者从游戏资源


scala.io.Source.fromURL(Play.resource(“/resource/file/packated/with/Play.txt”).get.getLines

您是在寻找Java还是Scala解决方案

在Scala中,您可以通过这种方式(无需外部依赖项)读取文件


scala.io.Source.fromFile(“/path/to/file.txt”).getLines

或者从游戏资源


scala.io.Source.fromURL(Play.resource(“/resource/file/packated/with/Play.txt”).get.getLines

在Windows中,传递文件路径时不要忘记添加两个“\”。 例如:

    File file = new File(""); // to create an empty file 
    // we suppose that the file in the public folder 
    File file_to_read = new File(file.getAbsolutePath() + "\\public\\filename.txt");

祝您好运

当您在Windows中时,在传递文件路径时不要忘记添加两个“\”。 例如:

    File file = new File(""); // to create an empty file 
    // we suppose that the file in the public folder 
    File file_to_read = new File(file.getAbsolutePath() + "\\public\\filename.txt");

祝你好运

你甚至可以导入Apache commons,让它更简单:你甚至可以导入Apache commons,让它更简单:刚刚得到这个:刚刚得到这个: