Loading a properties file from Java package

2019-01-01 12:37发布

I need to read a properties files that's buried in my package structure in 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.

9条回答
一个人的天荒地老
2楼-- · 2019-01-01 12:58

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.

        Properties properties = new Properties();
        InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
        properties.load(in);
        in.close();

Enjoy

查看更多
何处买醉
3楼-- · 2019-01-01 13:06

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:

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

(Exception handling elided, as before.)

查看更多
高级女魔头
4楼-- · 2019-01-01 13:08

When loading the Properties from a Class in the package com.al.common.email.templates you can use

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 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.

Using java.lang.String.class.getResource("foo.txt") would search for the (inexistent) file /java/lang/String/foo.txt on the classpath.

Using an absolute path (one that starts with '/') means that the current package is ignored.

查看更多
梦醉为红颜
5楼-- · 2019-01-01 13:12

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

查看更多
梦寄多情
6楼-- · 2019-01-01 13:17
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());
        }

    }
}
查看更多
呛了眼睛熬了心
7楼-- · 2019-01-01 13:19

Assuming your using the Properties class, via its load method, and I guess you are using the ClassLoader getResourceAsStream to get the input stream.

How are you passing in the name, it seems it should be in this form: /com/al/common/email/templates/foo.properties

查看更多
登录 后发表回答