我的webapps // WEB-INF / classes文件夹中有两个文件(一个.properties
和另一个.json
)。我需要引用.json
文件中的.properties
文件。由于两者都位于同一位置,propertyName=File.json
应该有效,但事实并非如此。如果我提到绝对路径,它就可以正常工作。
请建议。
答案 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后,您可以参考以下问题: