java 读取properties配置文件

直接上代码:

package com.base.jet.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtils {
    private Properties properties;
    private static PropertiesUtils propertiesUtils = new PropertiesUtils();

    //私有构造,禁止直接创建
    private PropertiesUtils() {
        properties = new Properties();
        InputStream in = PropertiesUtils.class.getClassLoader().getResourceAsStream("config.properties");
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取单例
    public static PropertiesUtils getInstance() {
        if (propertiesUtils == null) {
            propertiesUtils = new PropertiesUtils();
        }
        return propertiesUtils;
    }

    //服务器ip
    public String getServerIpAddress() {
        return properties.getProperty("server.ipAddress");
    }

    //服务器端口
    public String getServerPort() {
        return properties.getProperty("server.Port");
    }

    //是否是调试模式
    public Boolean getServerIsDebug() {
        return properties.getProperty("server.isDebug").equals("1");
    }

    //密码最大错误次数
    public int getPwdRrrorCounts() {
        return Integer.parseInt(properties.getProperty("user.pwdRrrorCounts"));
    }

    //系统版本号
    public String getSysVersion() {
        return properties.getProperty("sys.version");
    }

    public static void main(String[] args) {
        PropertiesUtils pro = PropertiesUtils.getInstance();
        System.out.println("http://" + pro.getServerIpAddress() + ":" + pro.getServerPort() + "/");
    }
}

猜你喜欢

转载自www.cnblogs.com/lixingwu/p/9249120.html