04 读取配置文件

在这里插入图片描述

  1. Maven工程的项目目录下新建ProUtil.java的文件
    在这里插入图片描述
    内容如下:
    package com.aliyun.chandao;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ProUtil {
private Properties prop;
private String filePath;
/**
* 构造方法
* */
public ProUtil(String filePath){
this.filePath = filePath;
this.prop = readProperties();
}

/**
 * 读取配置文件
 * */
private Properties readProperties(){
	Properties properties = new Properties();
	try {
		InputStream inputStream = new FileInputStream(filePath);
		BufferedInputStream in = new BufferedInputStream(inputStream);
		properties.load(in);
		
	} catch (IOException e) {
		e.printStackTrace();
	}
	return properties;
}

/**
 * 根据key读取关键字内容
 * */
public String  getPro(String key){
	if(prop.containsKey(key)){
		String username = prop.getProperty(key);
		return username;
	}else{
		System.out.println("你获取key值不对");
		return "";
	}	
}

}

  1. Maven工程目录下新建element.properties的文件
    在这里插入图片描述
    文件中内容如下:
    在这里插入图片描述
  2. 登录脚本中引用
    //实例化 ProUtil类
    ProUtil properties = new ProUtil(“element.properties”);
    //获取用户名输入框的定位方式
    String locator = properties.getPro(“username”);
    String localtorType = locator.split(">")[0];
    String localtorValue = locator.split(">")[1];
  3. 定位方式更改
    更新为: