我是 Play 框架的新手。我想读取conf文件夹中的一个文件。所以我用了
Play.application().classloader().getResources("Data.json").nextElement().getFile()
但我知道 play.Play 现在已被弃用。我可以用什么来读取该文件。
我读this https://www.playframework.com/documentation/2.5.x/Migration25#Deprecated-play.Play-and-play.api.Play-methods文章,几乎看不懂它说的是什么。
只需将应用程序注入到您需要的类中即可。假设它在控制器中:
import play.Application;
import javax.inject.Inject;
import javax.inject.Provider;
class YourController extends Controller {
@Inject
Provider<Application> app;
public Result someMethod() {
// (...)
// File is placed in conf/Data.json
InputStrem is = app.get().classloader().getResourceAsStream("Data.json");
String json = new BufferedReader(new InputStreamReader(is))
.lines().collect(Collectors.joining("\n"));
return ok(json).as("application/json");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)