我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates
。
我已经尝试了一切,但我无法弄清楚。
最后,我的代码将在 servlet 容器中运行,但我不想依赖容器做任何事情。我编写 JUnit 测试用例,它需要在两者中都工作。
我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates
。
我已经尝试了一切,但我无法弄清楚。
最后,我的代码将在 servlet 容器中运行,但我不想依赖容器做任何事情。我编写 JUnit 测试用例,它需要在两者中都工作。
从包中的类加载属性时,com.al.common.email.templates
您可以使用
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
(添加所有必要的异常处理)。
如果您的类不在该包中,则需要稍微不同地获取 InputStream:
InputStream in =
getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
/ 中的相对路径(没有前导“/”的路径)getResource()
意味着getResourceAsStream()
将相对于表示类所在的包的目录搜索资源。
使用java.lang.String.class.getResource("foo.txt")
将在类路径上搜索(不存在的)文件/java/lang/String/foo.txt
。
使用绝对路径(以“/”开头的路径)意味着忽略当前包。
要添加到 Joachim Sauer 的答案,如果您需要在静态上下文中执行此操作,您可以执行以下操作:
static {
Properties prop = new Properties();
InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
prop.load(in);
in.close()
}
(和以前一样,异常处理被忽略了。)
以下两种情况与从名为TestLoadProperties
.
案例 1:使用加载属性文件ClassLoader
InputStream inputStream = TestLoadProperties.class.getClassLoader()
.getResourceAsStream("A.config");
properties.load(inputStream);
在这种情况下,属性文件必须在root/src
目录中才能成功加载。
案例2:加载属性文件而不使用ClassLoader
InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
在这种情况下,属性文件必须与TestLoadProperties.class
成功加载的文件位于同一目录中。
注意: TestLoadProperties.java
和TestLoadProperties.class
是两个不同的文件。前者,.java
文件,通常在项目的src/
目录中,而后者,.class
文件,通常在其bin/
目录中。
public class Test{
static {
loadProperties();
}
static Properties prop;
private static void loadProperties() {
prop = new Properties();
InputStream in = Test.class
.getResourceAsStream("test.properties");
try {
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public class ReadPropertyDemo {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(
"com/technicalkeeda/demo/application.properties"));
System.out.println("Domain :- " + properties.getProperty("domain"));
System.out.println("Website Age :- "
+ properties.getProperty("website_age"));
System.out.println("Founder :- " + properties.getProperty("founder"));
// Display all the values in the form of key value
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println("Key:- " + key + "Value:- " + value);
}
} catch (IOException e) {
System.out.println("Exception Occurred" + e.getMessage());
}
}
}
假设您使用Properties类,通过它的load方法,我猜您正在使用 ClassLoader getResourceAsStream来获取输入流。
你是怎么传入名字的,看来应该是这样的形式:/com/al/common/email/templates/foo.properties
我设法通过这个电话解决了这个问题
Properties props = PropertiesUtil.loadProperties("whatever.properties");
另外,您必须将whatever.properties 文件放在/src/main/resources
没有人提到与上面类似但甚至更简单的解决方案,而无需处理类的包。假设 myfile.properties 在类路径中。
Properties properties = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
properties.load(in);
in.close();
享受
请使用以下代码:
属性 p = new Properties();
StringBuffer path = new StringBuffer("com/al/common/email/templates/");
path.append("foo.properties");
InputStream fs = getClass().getClassLoader()
.getResourceAsStream(path.toString());
if(fs == null){
System.err.println("Unable to load the properties file");
}
else{
try{
p.load(fs);
}
catch (IOException e) {
e.printStackTrace();
}
}