39 lines
1.3 KiB
Java
39 lines
1.3 KiB
Java
package com.inmind.properties06;
|
||
|
||
import java.util.Properties;
|
||
import java.util.Set;
|
||
|
||
/*
|
||
Properties 的特点与基本使用(重点)
|
||
|
||
构造方法
|
||
public Properties() :创建一个空的属性列表。
|
||
|
||
常用的存储方法
|
||
public Object setProperty(String key, String value) : 保存一对属性。
|
||
public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
|
||
public Set<String> stringPropertyNames() :所有键的名称的集合。 类似keySet()
|
||
|
||
注意:
|
||
1.Properties类似一个HashMap
|
||
2.它的真正作用是,能够将指定的配置文件的信息读取到properties对象中
|
||
*/
|
||
public class Demo17 {
|
||
public static void main(String[] args) {
|
||
//1.创建属性集对象
|
||
Properties properties = new Properties();
|
||
//2.保存属性集
|
||
properties.setProperty("username","admin");
|
||
properties.setProperty("password", "1234");
|
||
System.out.println(properties);//toString()
|
||
//3.获取属性值
|
||
String username = properties.getProperty("username");
|
||
String password = properties.getProperty("password");
|
||
System.out.println(username);
|
||
System.out.println(password);
|
||
//4.获取属性集的所有键
|
||
Set<String> keys = properties.stringPropertyNames();
|
||
System.out.println(keys);
|
||
}
|
||
}
|