properties配置文件
一、新建properties配置文件
右击包,new--->file,,弹出如下界面,输入文件名,后缀名为properties
二、配置文件中的内容
配置文件中输入,你要配置的内容。下面是工作中的一个例子
urlss=http://192.168.41.230:9980/ESB-2.0.0/services/soaWsConService?wsdl
cmemberCode=S000039
cmemberOcode=shgy0001
memberCode=GLB0156
operatorMember=GLB0156
memberName=\u52FE\u5D4A\u6751\u94A2\u6750\u79D1\u6280\u80A1\u4EFD\u6709\u9650\u516C\u53F8
pminfoListMoney=10
memberBcode=GLB0268
memberBname=\u6CF1\u6CF1\u80A1\u7968\u79D1\u6280\u80A1\u4EFD\u6709\u9650\u516C\u53F8
三、读取配置文件
Properties prop = new Properties();
try {
// 读取属性文件a.properties
//读取路径
String urlPath=Thread.currentThread().getContextClassLoader().getResource("").toString();//获取到URL路径
//再次读取路径
String path=urlPath.substring(urlPath.indexOf("/")+1);//获取到路径
//l数据流,传递数据
InputStream in = new BufferedInputStream(new FileInputStream(path+"pay.properties"));
//一行行的传
prop.load(in); // /加载属性列表
//Iterator<String> it = prop.stringPropertyNames().iterator();
//System.out.println(prop.getProperty("bieren"));
/*while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + prop.getProperty(key));
}*/
//启动IE浏览器
System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
//打开网址
driver.get("http://192.168.41.230:83/payexchange-2.0.0/addpaymentlistQuery.htm");
//设置等待时间
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//最大化
driver.manage().window().maximize();
WebElement urlss = driver.findElement(By.id("urlss"));
urlss.clear();
urlss.sendKeys(prop.getProperty("urlss"));
WebElement cmemberCode = driver.findElement(By.id("cmemberCode"));
cmemberCode.clear();
cmemberCode.sendKeys(prop.getProperty("cmemberCode"));
//定位流水号输入栏
WebElement opBillno = driver.findElement(By.id("opBillno"));
opBillno.clear();
//自定义输入输入时间格式
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
//获取当前电脑时间
Date currentDate=new Date();
//将当前时间格式化
String opBill=sdf.format(currentDate);
//将格式化后的时间输入到流水号蓝栏
opBillno.sendKeys(opBill);
WebElement cmemberOcode = driver.findElement(By.id("cmemberOcode"));
cmemberOcode.clear();
cmemberOcode.sendKeys(prop.getProperty("cmemberOcode"));
WebElement memberCode = driver.findElement(By.id("memberCode"));
memberCode.clear();
memberCode.sendKeys(prop.getProperty("memberCode"));
WebElement memberName = driver.findElement(By.id("memberName"));
memberName.clear();
memberName.sendKeys(prop.getProperty("memberName"));
WebElement pminfoListMoney = driver.findElement(By.id("pminfoListMoney"));
pminfoListMoney.clear();
pminfoListMoney.sendKeys(prop.getProperty("pminfoListMoney"));
WebElement memberBcode = driver.findElement(By.id("memberBcode"));
memberBcode.clear();
memberBcode.sendKeys(prop.getProperty("memberBcode"));
WebElement memberBname = driver.findElement(By.id("memberBname"));
memberBname.clear();
memberBname.sendKeys(prop.getProperty("memberBname"));
WebElement operatorMember = driver.findElement(By.id("operatorMember"));
operatorMember.clear();
operatorMember.sendKeys(prop.getProperty("operatorMember"));
WebElement leaveMessageForm=driver.findElement(By.className("subbtn"));
leaveMessageForm.click();
in.close();
}
catch (Exception e) {
System.out.println(e);
}
解析:try 先执行try方法中的内容。
如果try方法行不通,则执行catch方法
try方法中实现了properties配置文件的读取