方法一:
1.配置文件student.properties
name:张三
age:20
sex:man
2.配置文件读取工具类StudentUtil.java
public class StudentUtil
{
private static Properties props = new Properties();
static {
try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("student.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
return props.getProperty(key);
}
public static void updateProperties(String key, String value) {
props.setProperty(key, value);
}
}
3.使用工具类读取配置文件
public static void main(String[] args) {
//读取配置
String name=StudentUtil.getValue("name");
String age =StudentUtil.getValue("age");
}