读取配置类

package com.tl.spider.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

/**
 * @ClassName ReadConfigUtil
 * @Description 配置文件的读取类
 * @Author Administrator
 * @Date 2019/5/19 16:24
 * @Version 1.0
 **/
public class ReadConfigUtil {
    private String configPath;
    private Properties property;

    /**
     * 
     * @param configPath
     * @throws Exception
     */
    public ReadConfigUtil(String configPath) throws Exception {
        this.configPath = configPath;
        
        InputStream is = ReadConfigUtil.class.getClassLoader().getResourceAsStream(this.configPath);
        Reader reader = new InputStreamReader(is, StaticValue.ENCODING_DEFAULT);
        property = new Properties();
        property.load(reader);
        reader.close();
    }

    /**
     * 读取具体的某项配置
     * @param key
     * @return
     */
    public String getValue(String key) {
        return property.getProperty(key);
    }

    public static void main(String[] args) throws Exception {
        String configPath = "spider.properties";
        ReadConfigUtil readConfigUtil = new ReadConfigUtil(configPath);
        System.out.println(readConfigUtil.getValue("database_url"));
        System.out.println(readConfigUtil.getValue("database_userame"));
        System.out.println(readConfigUtil.getValue("database_password"));
        
    }
}

  

猜你喜欢

转载自www.cnblogs.com/wylwyl/p/10889757.html