java 配置项_【脚本写作】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

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

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值