【发布时间】:2010-09-24 21:42:39
【问题描述】:
我需要读取隐藏在 com.al.common.email.templates 中的包结构中的属性文件。
我已经尝试了所有方法,但我无法弄清楚。
最后,我的代码将在 servlet 容器中运行,但我不想依赖容器做任何事情。我编写了 JUnit 测试用例,它需要同时在这两种情况下工作。
【问题讨论】:
标签: java properties-file
我需要读取隐藏在 com.al.common.email.templates 中的包结构中的属性文件。
我已经尝试了所有方法,但我无法弄清楚。
最后,我的代码将在 servlet 容器中运行,但我不想依赖容器做任何事情。我编写了 JUnit 测试用例,它需要同时在这两种情况下工作。
【问题讨论】:
标签: java properties-file
从包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()
}
(和以前一样省略了异常处理。)
【讨论】:
resources 项目中resources 文件夹中的属性文件
以下两种情况与从名为 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());
}
}
}
【讨论】:
假设您通过 load 方法使用 Properties 类,我猜您正在使用 ClassLoader getResourceAsStream 获取输入流。
你是怎么传入名字的,好像应该是这样的形式:/com/al/common/email/templates/foo.properties
【讨论】:
我设法通过这个电话解决了这个问题
Properties props = PropertiesUtil.loadProperties("whatever.properties");
另外,你必须把你的whatever.properties文件放在/src/main/resources
【讨论】:
PropertiesUtil?
没有人提到与上面类似但甚至更简单的解决方案,无需处理类的包。假设 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();
}
}
【讨论】: