无法在Java中加载属性文件 [英] Not able to load properties file in Java

查看:114
本文介绍了无法在Java中加载属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载属性文件。适当的文件位于应用程序的类路径中。

I am trying to load a properties file. The properites file is in the class path of the application.

 Properties p = new Properties();
 p.load(new FileInputStream("classpath:mail.properties"));
 System.out.println(p.get("hi"));

现在我说classpath,因为另一个名为x.properties的文件在xml文件中被引用,就像这样

Now I say classpath, because another file called x.properties is referred in an xml file like this

<property name="x">
    <util:properties location="classpath:x.properties" />
</property>

我将mail.properties放在与x.properties相同的文件夹中,但我的Java程序不是能找到吗?知道我缺少什么吗?

I placed my mail.properties in the same folder as x.properties, but my Java program is not able to find it ? Any idea what I am missing ?

推荐答案

仅仅因为某些程序处理该XML文件喜欢的语法 classpath:x.properties 并不意味着它是普遍接受的语法 in Java

Just because some program processing that XML file likes the syntax classpath:x.properties doesn't mean that it is a universally accepted syntax in Java!

如果您将classpath:x.properties提供给 FileInputStream ,它将寻找名为 classpath:x.properties 的文件。 (查看该特定构造函数的文档。)

If you provide "classpath:x.properties" to a FileInputStream it will look for a file named classpath:x.properties. (Check the documentation of that particular constructor.)

尝试提供该文件的完整路径。如果文件恰好在您的类路径上,您可以使用类似

Try providing the full path to that file. If the file happens to be on your class path, you could use something like

p.load(getClass().getResourceAsStream("mail.properties"));

这篇关于无法在Java中加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆