一、代码Demo
-
读取的配置文件目录结构及内容 -
读取文件配置类
public class MyPropertyUtil {
public static final Properties properties = new Properties();
static {
InputStream resourceAsStream = DemoApplication.class.getClassLoader().getResourceAsStream("user.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 测试Demo
public class GetPropDemo {
private static String userName;
private static String userAge;
static {
userName = MyPropertyUtil.properties.getProperty("user.name");
userAge = MyPropertyUtil.properties.getProperty("user.age");
}
public static void main(String[] args) {
System.out.println("username->" + userName);
System.out.println("userage->" + userAge);
}
}
结果如下:
文章仅供学习交流,侵权联系删除。
|