私は次のファイルを持っています
/app/menus/menu1.yml
内容を読みたい
-
短い答え:
fileContent = play.vfs.VirtualFile.fromRelativePath("/app/menus/menu1.yml").contentAsString();
私は次のファイルを持っています
/app/menus/menu1.yml
内容を読みたい
-
短い答え:
fileContent = play.vfs.VirtualFile.fromRelativePath("/app/menus/menu1.yml").contentAsString();
PlayFramework は Java 言語を使用して構築されています。
コードでは、Java API の使用に関する制限はありません。したがって、ファイルの絶対パスがわかっている場合は、標準の Java コードを使用してファイルを読み取ることができます。
java.io.File yourFile = new java.io.File("/path/app/menus/menu1.yml");
java.io.FileReader fr = new java.io.FileReader(yourFile);
// etc.
Play アプリケーションから相対パスでファイルにアクセスする場合は、play "VirtualFile" クラスを使用できます: 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.
Scala で Play 2.0 を使用する場合Play.getFile(relativePath: String)
PlayにはSnakeYAMLパーサーが含まれています。彼らのドキュメントから:
Yaml yaml = new Yaml();
String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
List<String> list = (List<String>) yaml.load(document);
System.out.println(list);
['セセリチョウ科'、'アゲハチョウ科'、'ツバメガ科'、'ツバメガ科']
を使用するバージョンもありYaml.load
ますInputStream
。これは、次のサンプルコードで示されています:http ://code.google.com/p/snakeyaml/source/browse/src/test/java/examples/LoadExampleTest.java