但行好事
莫论前程❤

PropertiesUtil参考

application.properties文件内容如下:

cluster.enable=true
cluster.web.tcp.port=9997
#cluster.zookeeper.address=10.10.202.157,10.10.202.158,10.10.202.159
cluster.zookeeper.address=192.168.156.102,192.168.156.103,192.168.156.104
cluster.zookeeper.sessiontimeout=30000
cluster.zookeeper.retry.interval=60000
cluster.connect.address=

property读取properties文件时,默认将其存储到map中
工具类具体代码

package com.spring.base.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

@SuppressWarnings("unchecked")
public class PropertiesUtil {

    @SuppressWarnings("rawtypes")
    private static Hashtable hash = new Hashtable();
    private static final  String FILENAME = "application.properties";
    static{
        hash.put("config",FILENAME);
    }
    private PropertiesUtil() {

    }
    /**
     * 得到文件内容
     * 
     * @param fileName
     * @return
     */
    @SuppressWarnings("resource")
    private static Properties getProperties(String fileName) {
        InputStream input = null;
        Properties prop = null;
        try {
            prop = (Properties) hash.get(fileName);
            if (prop == null) {
                try {
                    input = new FileInputStream(fileName);// 全路径
                } catch (Exception ex) {
                    if (fileName.startsWith("/")) {
                        input = PropertiesUtil.class.getResourceAsStream(fileName);// 只有/+文件名字
                    } else {
                        input = PropertiesUtil.class.getResourceAsStream("/" + fileName);// 只有文件名字的
                    }
                }
                prop = new Properties();
                prop.load(input);
                if (input != null) {
                    input.close();
                }
            }
        } catch (Exception e) {
            return null;
        }
        return prop;
    }

    /**
     * 通过key得到值
     * 
     * @param key
     * @return
     */
    public static String getProperty(String key,String fileKey) {
        try {
            String fileName = (String) hash.get(fileKey);
            Properties prop = PropertiesUtil.getProperties(fileName);
            if (null == prop)
                return null;
            return prop.getProperty(key);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 通过key得到拥有默认的值
     * 
     * @param key
     * @return
     */
    public static String getProperty(String key, String defaultValue,String fileKey) {
        try {
            String fileName = (String) hash.get(fileKey);
            Properties prop = PropertiesUtil.getProperties(fileName);
            if (null == prop)
                return null;
            return prop.getProperty(key, defaultValue);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 通过key得到数组值
     * 
     * @param key
     * @return
     */
    public static String getProperty(String key, String[] array,String fileKey) {
        try {
            String fileName = (String) hash.get(fileKey);
            Properties prop = PropertiesUtil.getProperties(fileName);
            if (null == prop)
                return null;
            String str = prop.getProperty(key);
            for (int i = 0; i < array.length; i++) {
                str = str.replaceAll("\\{" + (i + 1) + "\\}", array[i]);
            }
            return str;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 对sql模板进行包装
     * @return
     */
    public static String warpSQL(String sqlTemplate,String warpContent){
        sqlTemplate = sqlTemplate.replaceAll("\\{1\\}",warpContent);
        return sqlTemplate;
    }

    public static void main(String[] args) {
//      String str = warpSQL(getProperty("sqlserver2005","sqltemplate"),"select row_number() over(order by id) as rownum,* from mt200909 where 1=1");
        while(true){
            String str = getProperty("cluster.enable","","config");
            System.out.println(str);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

示例2

该文件位于项目根路径下:

url=jdbc:oracle:thin:@10.230.154.215:1521:ora12c
username=urapport_config
password=a
maxpoolsize=30
minpoolsize=5

java代码:

        FileInputStream fis = null;

        try {
            fis = new FileInputStream("dbconfig.properties");
            Properties properties = new Properties();
            properties.load(fis);
            user = properties.getProperty("username");
            password = properties.getProperty("password");
            jdbcUrl = properties.getProperty("url");
            maxPoolSize = Integer.valueOf(properties.getProperty("maxpoolsize"));
            minPoolSize = Integer.valueOf(properties.getProperty("minpoolsize"));
            String isEncryption = properties.getProperty("isEncryption");
            if ("true".equals(isEncryption)) {
                user = EncryptForDbData.decrypt(user);
                password = EncryptForDbData.decrypt(password);
            }

            tracer.special("ConfigDbConfig setValues end!");
        } catch (FileNotFoundException var13) {
            tracer.error("NO such file ", var13);
        } catch (IOException var14) {
            tracer.error("IO Exception ", var14);
        } finally {
            try {
                fis.close();
            } catch (Exception var12) {
                var12.printStackTrace();
            }

        }
赞(1) 打赏
未经允许不得转载:刘鹏博客 » PropertiesUtil参考
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏