该空间是个人实践过程中的总结,欢迎交流吐槽!

【脚本写作】java:读取指定配置文件-自定义配置项

上一篇 / 下一篇  2016-09-08 15:02:38 / 个人分类:Java

src/main/resource/config.properties文件配置示例(横线之间)
================================================================
# 测试环境:online or offline
test_environment=offline
test_data_delete=false

# webSocket请求参数
websocket_connection_auth_user=admin
websocket_connection_auth_password=123456
websocket_connection_domain=www.example.test.com
websocket_connection_port=9527
websocket_connection_token=6C12137C81F74F782A0FF2DCF3AA31817E0CD79E7F4ABD952462C7424D7EBB19E7387CAECD55EA3EBF29F7EB427FDA6D0A383BA3E8AB343DCEF291F2AFE6A6DC891029468B577EF12CF99903C0520F615B2854CB10E72D284388488EDF65B8A20F8FA0E9C4992798028F3210845E5EAD1C15BE99C514474500E9A7B6A0689D58CAF47BA496FDEFC661819BF0A4FF88C0
websocket_connection_hostname=192.168.0.1

websocket_connection_network_timeout=5000
websocket_connection_time_nextrequest=1000

================================================================

创建java
package funcLaunDesktop.common;

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

/**
 * 读取配置文件
 *
 * @author Janesong on 2016-4-19
 **/
public class TestConfigUtil {
    private static Properties config;
    private static String NLINE = "online";
    private static TestConfigUtil instance = null;

    private TestConfigUtil() {
        try {
            InputStream is = ClassLoader.getSystemResourceAsStream("config.properties");
//InputStream is = TestConfigUtil.class.getClassLoader().getResourceAsStream("test.properties");
            config = new Properties();
            config.load(is);
        } catch (Exception exLog) {
            exLog.printStackTrace();
        }
    }

    /**
     * 单例模式,获取TestConfigUtil对象
     *
     * @return
     */
    public static TestConfigUtil getInstance() {
        if (instance == null) {
            instance = new TestConfigUtil();
        }
        return instance;
    }

    /**
     * 读取配置文件中的汉字
     *
     * @param text
     * @return String
     */
    private String TransferUTF8(String text) {
        String result = text;
        try {
            result = new String(text.getBytes("ISO-8859-1"), "UTF-8");
        } catch (Exception exLog) {
            exLog.printStackTrace();
        }
        return result;
    }

    /**
     * 读取配置文件中测试环境变量
     *
     * @return
     */
    public String getTestEnvironment() {
        String testEnvironment = config.getProperty("test_environment");
        return testEnvironment.toLowerCase();
    }

    /**
     * 是否删除测试产生的数据
     *
     * @return
     */
    public String getTestDataDelete() {
        String browser = config.getProperty("test_data_delete");
        return browser.toLowerCase();
    }

    /**
     * 读取配置文件参数的值
     *
     * @param property
     * @return String
     */
    private String getPropertyValue(String property) {
        String result = config.getProperty(property);
        if (result != null) {
            result.trim();
            if (getTestEnvironment().equals(ONLINE)) {
                result = result.replace("offline", "online");
            }
        }

        return result;
    }

    /*
    * 读取配置文件中测试浏览器变量
    */
    public String getTestBrowser() {
        String browser = config.getProperty("test_browser");
        return browser.toLowerCase();
    }

    /**
     * 读取server端:操作账户
     *
     * @return String
     */
    public String getServerAPIsOperator() {
        String serverOperator = "";
        serverOperator = getPropertyValue("websocket_connection_auth_user");
        return TransferUTF8(serverOperator);                // 可实现对中文字符编码转换
    }

    /**
     * 读取server端:操作账户密码
     *
     * @return String
     */
    public String getServerAPIsOperatePasswd() {
        String serverOperatorPasswd = "";
        serverOperatorPasswd = getPropertyValue("websocket_connection_auth_password");
        return serverOperatorPasswd;
    }


    /**
     * 读取WebSocket属性参数:域名地址
     *
     * @return String
     */
    public String getWebSocketDomain() {
        String wsDomain = "";
        wsDomain = getPropertyValue("websocket_connection_domain");
        return wsDomain;
    }

    /**
     * 读取WebSocket属性参数:服务端口号
     *
     * @return String
     */
    public String getWebSocketPort() {
        String wsPort = "";
        wsPort = getPropertyValue("websocket_connection_port");
        return wsPort;
    }

    /**
     * 读取WebSocket属性参数:token
     *
     * @return String
     */
    public String getWebSocketToKen() {
        String wsToken = "";
        wsToken = getPropertyValue("websocket_connection_token");
        return wsToken;
    }

    /**
     * 读取WebSocket属性参数:ip -- 登录的hostname
     *
     * @return String
     */
    public String getWebSocketHostName() {
        String hostName = "";
        hostName = getPropertyValue("websocket_connection_hostname");
        return hostName;
    }

 
    /**
     * 读取WebSocket属性参数:连接网络超时
     *
     * @return int
     */
    public int getWebSocketNextRequestTime() {
        String waitTime = getPropertyValue("websocket_connection_time_nextrequest");
        if (waitTime != null) {
            return Integer.parseInt(waitTime);
        }
        return -1;
    }

    /**
     * 读取WebSocket属性参数:连接网络超时
     *
     * @return int
     */
    public int getWebSocketConnectTimeOut() {
        String timeOut = getPropertyValue("websocket_connection_network_timeout");
        if (timeOut != null) {
            return Integer.parseInt(timeOut);
        }
        return -1;
    }


}


TAG: java Java 配置文件

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 17833
  • 日志数: 16
  • 建立时间: 2016-09-08
  • 更新时间: 2018-03-28

RSS订阅

Open Toolbar