You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							115 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
	
	
							115 lines
						
					
					
						
							2.8 KiB
						
					
					
				package com.ynxbd.common.helper;
 | 
						|
/*
 | 
						|
 * Copyright (c) 2022
 | 
						|
 * User:Administrator
 | 
						|
 * File:ProperHelper.java
 | 
						|
 * Date:2022/06/13 14:56:13
 | 
						|
 */
 | 
						|
 | 
						|
import java.io.InputStream;
 | 
						|
import java.util.Properties;
 | 
						|
 | 
						|
public class ProperHelper {
 | 
						|
 | 
						|
    private Properties properties;
 | 
						|
 | 
						|
    private boolean isEnable = true;
 | 
						|
 | 
						|
    /**
 | 
						|
     * 读取properties配置文件...\project\web\WEB-INF\classes\xx.properties
 | 
						|
     *
 | 
						|
     * @param fileName 文件名
 | 
						|
     * @return Properties对象,通过getProperty()获取值
 | 
						|
     */
 | 
						|
    public ProperHelper read(String fileName) {
 | 
						|
        properties = new Properties();
 | 
						|
        try (InputStream in = ProperHelper.class.getClassLoader().getResourceAsStream(fileName)) {
 | 
						|
            properties.load(in);
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
        }
 | 
						|
        return this;
 | 
						|
    }
 | 
						|
 | 
						|
    public synchronized void setIsEnable(String rootName) {
 | 
						|
        this.isEnable = getBoolean(rootName, false);
 | 
						|
    }
 | 
						|
 | 
						|
    public synchronized void setIsEnable(boolean isEnable) {
 | 
						|
        this.isEnable = isEnable;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取配置文件String值
 | 
						|
     *
 | 
						|
     * @param rootNode 节点名
 | 
						|
     * @return String
 | 
						|
     */
 | 
						|
    public String getString(String rootNode) {
 | 
						|
        return getString(rootNode, null);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取配置文件String值
 | 
						|
     *
 | 
						|
     * @param rootNode 节点名
 | 
						|
     * @return String
 | 
						|
     */
 | 
						|
    public String getString(String rootNode, String defaultVal) {
 | 
						|
        try {
 | 
						|
            if (properties == null) {
 | 
						|
                return defaultVal;
 | 
						|
            }
 | 
						|
 | 
						|
            if (!isEnable) {
 | 
						|
                return defaultVal;
 | 
						|
            }
 | 
						|
 | 
						|
            String val = properties.getProperty(rootNode);
 | 
						|
            return val == null ? defaultVal : val.trim();
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取配置文件Boolean值,可设默认值
 | 
						|
     *
 | 
						|
     * @param rootNode 节点名
 | 
						|
     * @return Boolean
 | 
						|
     */
 | 
						|
    public boolean getBoolean(String rootNode, boolean defaultVal) {
 | 
						|
        try {
 | 
						|
            if (properties == null) {
 | 
						|
                return defaultVal;
 | 
						|
            }
 | 
						|
 | 
						|
            String val = properties.getProperty(rootNode);
 | 
						|
            return val == null ? defaultVal : Boolean.parseBoolean(val.trim());
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return defaultVal;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取配置文件Integer值
 | 
						|
     *
 | 
						|
     * @param rootNode 节点名
 | 
						|
     * @return Integer
 | 
						|
     */
 | 
						|
    public Integer getInteger(String rootNode) {
 | 
						|
        try {
 | 
						|
            String val = getString(rootNode, null);
 | 
						|
            return val == null ? null : Integer.parseInt(val.trim());
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
}
 | 
						|
 |