Properties类
构造方法
public Properties()
:创建一个空的属性列表。
基本的存储方法
public Object setProperty(String key, String value)
: 保存一对属性。public String getProperty(String key)
:使用此属性列表中指定的键搜索属性值。public Set<String> stringPropertyNames()
:所有键的名称的集合。
与流相关的方法
public void load(InputStream inStream)
: 从字节输入流中读取键值对。
参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了
小贴士:文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。
举个栗子:
public static void testProperties(){
Properties properties = new Properties();
properties.setProperty("key","value");
System.out.println(properties);
properties.setProperty("key2","value2");
properties.forEach((key,value)->{
System.out.println("key:"+ key);
System.out.println("value:"+ value);
});
properties.setProperty("key3","value3");
properties.setProperty("key3","value3");
Set<String> keys = properties.stringPropertyNames();
keys.forEach(System.out::println);
File file = new File("C:\\Users\\admin\\Desktop\\test.txt");
try(FileInputStream fileInputStream = new FileInputStream(file);){
properties.load(fileInputStream);
System.out.println(properties);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}