从Java包加载属性文件

Sac*_*Joe 109 java properties-file

我需要读取一个隐藏在我的包结构中的属性文件com.al.common.email.templates.

我已经尝试了一切,我无法弄明白.

最后,我的代码将在servlet容器中运行,但我不想依赖于容器.我编写JUnit测试用例,它需要兼顾两者.

Joa*_*uer 232

从包中的类加载属性时,com.al.common.email.templates您可以使用

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();
Run Code Online (Sandbox Code Playgroud)

(添加所有必要的异常处理).

如果您的类不在该包中,则需要以稍微不同的方式获取InputStream:

InputStream in = 
 getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");
Run Code Online (Sandbox Code Playgroud)

相对路径(那些没有前导"/")中getResource()/ getResourceAsStream()意味着资源将相对于它表示包的类是在目录中搜索.

使用java.lang.String.class.getResource("foo.txt")将搜索/java/lang/String/foo.txt类路径上的(不存在的)文件.

使用绝对路径(以'/'开头的路径)意味着忽略当前包.

  • 建议:添加说明何时使用相对路径,何时使用绝对路径(开头有和没有“/”)。 (2认同)

小智 48

要添加Joachim Sauer的答案,如果您需要在静态环境中执行此操作,您可以执行以下操作:

static {
  Properties prop = new Properties();
  InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
  prop.load(in);
  in.close()
}
Run Code Online (Sandbox Code Playgroud)

(与以前一样,例外处理已被删除.)


Kul*_*eep 15

以下两种情况涉及从名为的示例类加载属性文件TestLoadProperties.

案例1:使用加载属性文件 ClassLoader

InputStream inputStream = TestLoadProperties.class.getClassLoader()
                          .getResourceAsStream("A.config");
properties.load(inputStream);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,属性文件必须位于root/src目录中才能成功加载.

案例2:不使用加载属性文件 ClassLoader

InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,属性文件必须与TestLoadProperties.class成功加载的文件位于同一目录中.

注意: TestLoadProperties.java并且TestLoadProperties.class是两个不同的文件.前一个.java文件通常位于项目的src/目录中,而后一个.class文件通常位于其bin/目录中.


小智 11

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();
    }

}
Run Code Online (Sandbox Code Playgroud)


Vic*_*cky 8

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());
        }

    }
}
Run Code Online (Sandbox Code Playgroud)