zoukankan      html  css  js  c++  java
  • How to parse project properties or how to parse files with key-value pair

    If a file has content like

    app.enabled = false
    app.host = "localhost"
    app.port = 8080
    app.zoneId = "zone_id"
    app.fulOpId = "test_uk_1"
    

    which are all key-value pairs, we could use properties.load() to parse it.

        public static Properties loadProperties() {
            Properties properties = new Properties();
            try {
                InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("project.properties");
                if(in==null) {
                    logger.warn("The project.properties file does not exist");
                } else {
                    properties.load(in);
                }
            } catch(Exception e) {
                logger.error("Unable to read the project.properties", e);
            }
            return properties;
        }
    

     The reason to use Thread.currentThread().getContextClassLoader().getResourceAsStream("project.properties") and why it is different from normal class loader:

    Each class will use its own classloader to load other classes. So if ClassA.class references ClassB.class then ClassB needs to be on the classpath of the classloader of ClassA, or its parents.
    
    The thread context classloader is the current classloader for the current thread. An object can be created from a class in ClassLoaderC and then passed to a thread owned by ClassLoaderD. In this case the object needs to use Thread.currentThread().getContextClassLoader() directly if it wants to load resources that are not available on its own classloader.
    
  • 相关阅读:
    《微风吹过的街道》Alpha冲刺Scrum meeting5
    《微风吹过的街道》Alpha冲刺Scrum meeting4
    小明分蛋糕题解
    小明分蛋糕(附题解)
    正确答案
    阴影面积
    双面打印
    实验十 团队作业6:团队项目用户验收&Beta冲刺
    你说什么都不对【Beta】Scrum meeting 4
    你说什么都不队【Beta】Scrum meeting 3
  • 原文地址:https://www.cnblogs.com/codingforum/p/java.html
Copyright © 2011-2022 走看看