无法将属性文件加载到java文件中
问题描述:
我无法将属性文件加载到java文件中。获取文件未找到异常。你能帮忙吗?无法将属性文件加载到java文件中
的Java文件位置: 类/ COM /我的/位置/为/ javabased /公共事业/转换器/的servlet/GetProp.java
属性文件位置: 类/ COM /我的/属性/性能/配置的.properties
我的代码:
Properties inputParams = new Properties();
FileInputStream in = new FileInputStream("classes/com/my/property/properties/Config.properties");
inputParams.load(in);
in.close();
获取文件未发现异常
答
我喜欢阅读属性文件在这样的类
public class SomeClass{
private static Properties someProperties = new Properties();
static{
someProperties.load(SomeClass.class.getResourceAsStream("/com/my/property/properties/Config.properties"));
}
希望这会有所帮助。
答
java类File(由FileInputStream使用)基于文件系统路径,无论是绝对路径还是相对于当前工作目录。两者大多不在正在运行的应用程序的完全控制之下。如果您的资源可以在类路径中找到(从正在运行的应用程序的角度来看,它总是相同的),您应该使用类加载器的资源加载机制(正如Vijendra Kulhade在他的回答中指出的那样)。
是否使用了相对于应用程序实际类路径的正确路径? –
请查看:http://stackoverflow.com/a/26358704/281815 – rbento
从路径中删除classes文件夹 – Jens