码迷,mamicode.com
首页 > 编程语言 > 详细

java 读取properties配置文件

时间:2018-07-01 01:06:12      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:public   IV   config   inpu   服务器ip   nts   ror   style   ddr   

直接上代码:

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() + "/");
    }
}

 

java 读取properties配置文件

标签:public   IV   config   inpu   服务器ip   nts   ror   style   ddr   

原文地址:https://www.cnblogs.com/lixingwu/p/9249120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!