【Java】Properties 配置信息类

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

猜你喜欢

转载自www.cnblogs.com/mindzone/p/12743479.html