首页 > 编程语言 > 详细

【Java】Properties 配置信息类

时间:2020-04-21 12:37:27      阅读:50      评论:0      收藏:0      [点我收藏+]

Properties 配置信息类

Properties 是HashTable的子类,该对象用于处理属性文件

由于属性文件的Key、Value都是字符串类型,所以Properties里的Key和Value也一样是String

存取数据时使用:

setProperty(String k,String v)

getProperty(String k)

 

要注意配置文件,xxx.properties要放在当前项目的目录下

技术分享图片

 

可以看到文件的信息已经可以被读取到

技术分享图片

 

 文件流的异常和加载方法的异常直接使用抛出处理

 1 public class PropertiesTest {
 2     public static void main(String[] args) throws IOException {
 3         // 创建配置信息对象
 4         Properties properties = new Properties();
 5 
 6         // 创建文件流对象,指定文件
 7         FileInputStream inputStream = new FileInputStream("jdbc.properties");
 8 
 9         // 加载流对象的信息
10         properties.load(inputStream);
11 
12         // 读取配置信息
13         String username = properties.getProperty("username");
14         System.out.println(username);
15 
16         String password = properties.getProperty("password");
17         System.out.println(password);
18     }
19 }

 

Properties文件

username = root
password = 123456
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mysql

 

【Java】Properties 配置信息类

原文:https://www.cnblogs.com/mindzone/p/12743479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!