如何在Tomcat的WEB-INF / classes文件夹中引用另一个文件?

时间:2016-09-29 10:23:29

标签: json tomcat web-applications tomcat7 properties-file

我的webapps // WEB-INF / classes文件夹中有两个文件(一个.properties和另一个.json)。我需要引用.json文件中的.properties文件。由于两者都位于同一位置,propertyName=File.json应该有效,但事实并非如此。如果我提到绝对路径,它就可以正常工作。

请建议。

1 个答案:

答案 0 :(得分:0)

如果您不想指定绝对路径,那么您应该以与属性文件完全相同的方式读取File.json。 如果propertyName = File.json:

String jsonFile = props.getProperty("propertyName");

然后,您可以执行以下操作将文件作为URL对象:

URL jsonURL = Thread.currentThread().getContextClassLoader().getResource(jsonFile);

或者作为InputStream

InputStream jsonStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(jsonFile);

根据您以后要做的事情,将文件作为InputStream或URL后,您可以参考以下问题: