Java实现对项目配置信息读取

系统配置信息读取工具

  • 读取Maven项目中resources目录下的配置文件,可以应用到一些需要动态获取的数据。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 系统配置信息读取工具
 * 
 * @author volitation
 *
 */
public class PropertiesUtil {

    private final static String CONFIG_PATH = "/conf/config.properties";

    /**
     * 获取链接地址
     * @param keyId
     * @return
     */
    public static String getConfigLinkPath(String keyId) {
        Properties proper = new Properties();
        InputStream in = null;

        try {
            in = PropertiesUtil.class.getClassLoader().getResourceAsStream(CONFIG_PATH);
            proper.load(in);

            Enumeration<?> en = proper.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                if (keyId.equals(key)) 
                    return proper.getProperty(key);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}

猜你喜欢

转载自blog.csdn.net/volitationlong/article/details/79898118
今日推荐