从 Java 包加载属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/333363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading a properties file from Java package
提问by SacramentoJoe
I need to read a properties files that's buried in my package structure in com.al.common.email.templates
.
我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates
。
I've tried everything and I can't figure it out.
我已经尝试了一切,但我无法弄清楚。
In the end, my code will be running in a servlet container, but I don't want to depend on the container for anything. I write JUnit test cases and it needs to work in both.
最后,我的代码将在 servlet 容器中运行,但我不想依赖容器做任何事情。我编写了 JUnit 测试用例,它需要在两者中工作。
采纳答案by Joachim Sauer
When loading the Properties from a Class in the package com.al.common.email.templates
you can use
从包中的类加载属性时,com.al.common.email.templates
您可以使用
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
(Add all the necessary exception handling).
(添加所有必要的异常处理)。
If your class is not in that package, you need to aquire the InputStream slightly differently:
如果您的类不在该包中,则需要稍微不同地获取 InputStream:
InputStream in =
getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
Relative paths (those without a leading '/') in getResource()
/getResourceAsStream()
mean that the resource will be searched relative to the directory which represents the package the class is in.
相对路径(那些没有前导“/”)中getResource()
/getResourceAsStream()
该资源将相对于它表示包的类是在目录中搜索平均值。
Using java.lang.String.class.getResource("foo.txt")
would search for the (inexistent) file /java/lang/String/foo.txt
on the classpath.
使用java.lang.String.class.getResource("foo.txt")
将/java/lang/String/foo.txt
在类路径上搜索(不存在的)文件。
Using an absolute path (one that starts with '/') means that the current package is ignored.
使用绝对路径(以“/”开头的路径)意味着当前包被忽略。
回答by Chris Kimpton
Assuming your using the Propertiesclass, via its loadmethod, and I guess you are using the ClassLoader getResourceAsStreamto get the input stream.
假设您使用Properties类,通过其load方法,我猜您正在使用 ClassLoader getResourceAsStream来获取输入流。
How are you passing in the name, it seems it should be in this form: /com/al/common/email/templates/foo.properties
你是怎么传名字的,好像应该是这个形式: /com/al/common/email/templates/foo.properties
回答by cobra libre
To add to Joachim Sauer's answer, if you ever need to do this in a static context, you can do something like the following:
要添加到 Joachim Sauer 的答案中,如果您需要在静态上下文中执行此操作,您可以执行以下操作:
static {
Properties prop = new Properties();
InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
prop.load(in);
in.close()
}
(Exception handling elided, as before.)
(和以前一样,省略了异常处理。)
回答by user897493
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();
}
}
回答by Vicky
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());
}
}
}
回答by KulDeep
The following two cases relate to loading a properties file from an example class named TestLoadProperties
.
以下两种情况与从名为 的示例类加载属性文件有关TestLoadProperties
。
Case 1: Loading the properties file using ClassLoader
案例 1:使用加载属性文件 ClassLoader
InputStream inputStream = TestLoadProperties.class.getClassLoader()
.getResourceAsStream("A.config");
properties.load(inputStream);
In this case the properties file must be in the root/src
directory for successful loading.
在这种情况下,属性文件必须位于root/src
成功加载的目录中。
Case 2: Loading the properties file without using ClassLoader
案例2:加载属性文件而不使用 ClassLoader
InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
In this case the properties file must be in the same directory as the TestLoadProperties.class
file for successful loading.
在这种情况下,属性文件必须与TestLoadProperties.class
成功加载的文件位于同一目录中。
Note:TestLoadProperties.java
and TestLoadProperties.class
are two different files. The former, .java
file, is usually found in a project's src/
directory, while the latter, .class
file, is usually found in its bin/
directory.
注意:TestLoadProperties.java
和TestLoadProperties.class
是两个不同的文件。前者,.java
文件,通常在项目的src/
目录中,而后者,.class
文件,通常在项目的bin/
目录中。
回答by Prithvish Mukherjee
use the below code please :
请使用以下代码:
Properties 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();
}
}
回答by Naramsim
I managed to solve this issue with this call
我设法通过这个电话解决了这个问题
Properties props = PropertiesUtil.loadProperties("whatever.properties");
Extra, you have to put your whatever.properties file in /src/main/resources
另外,你必须把你的whatever.properties 文件放在 /src/main/resources
回答by isaac.hazan
Nobody mentions the similar but even simpler solution than above with no need to deal with the package of the class. Assuming myfile.properties is in the classpath.
没有人提到类似但比上面更简单的解决方案,无需处理类的包。假设 myfile.properties 在类路径中。
Properties properties = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
properties.load(in);
in.close();
Enjoy
享受