一.特性
1/Properties集合 是Map集合的一种,因此它具有map集合的特性,比如[无序存取].
2/Properties的不存在泛型,默认隐性泛型为Object,但它的[键]和[值]必须都为String类型,因此如果要遍历它,泛型必须为String或Object.
3/Properties在日后的使用中频率很多,且独有方法很是强大,虽然继承自Map,但功能却相比其强大得多!
4/不推荐直接使用其实现的’put’来添加元素.而是推荐通过其特有方法setProperty(String,String).因为properties的[键]和[值]只能是String
创建格式:Properties 自定义对象名 = new Properties();
二.非常实用的特有方法
1/ 添加/修改(即:键重复则替换值)元素,跟hashmap集合一样.
格式:自定义对象名.setProperty(String型键,String型值);
列如:propertiesObj.setProperty(“中国”, “5500”);
2/ 根据键找到值,推荐用这个代替实现自Map的get方法.
格式:自定义对象名.getProperty(键);
列如:propertiesTwo.getProperty(key);
3/ 获取集合中的所有键,自然而然也是字符串.推荐用这个代替Map的keySet方法.它同样以Set集合为多态形式创建.
格式:自定义对象名.stringPropertyNames();
列如:Set a = propertiesTwo.stringPropertyNames();
4/ 以’字节’流的形式读取硬盘文件的键和元素,默认其正则分割符为’=’,因此文件内容必须是’键=值’的格式.
格式:自定义对象名.load(基本/缓冲字节输入流对象 如:BufferedInputStream);
列如:propertiesObj.load(bufferedInputStreamObj);
5/ 从文件中以’字符’流的形式读取硬盘文件的键和元素,默认其正则分割符为’=’,因此文件内容必须是’键=值’的格式.
格式:自定义对象名.load(基本/缓冲字符输入流对象 如:FileReader);
列如:propertiesTwo.load(fileReaderOfZifu);
6/ 将所有的键和值以’字节’流形式输出到硬盘文件中.
格式:自定义对象名.store(基本/缓冲字节输出流对象, 默认为null的备注信息);
列如:propertiesObj.store(bufferedOutputStreamObj,null);
7/ 将所有键和值以’字符’流形式输出到硬盘文件中.
格式:自定义对象名.store(基本/缓冲字符输出流对象,默认为null的备注信息)
列如:propertiesObj.store(fileWriterOfZiFu, “我是备注信息哦!文件中将看到我”);
普通应用
public static void main(String[] args) throws IOException {
Properties propertiesObj = new Properties();
propertiesObj.put("天下第一剑", "Properties");
propertiesObj.put("华夏威武健", "Properties");
propertiesObj.put("世界大同", "PropertiesADA");
System.out.println(propertiesObj);
//遍历Properties集合的键和值
Set<String> keySetObj = propertiesObj.stringPropertyNames();
for (String s : keySetObj) {
String a =s;
System.out.println("键为:"+a+" 值为:"+propertiesObj.getProperty(a));
}
//以字节流之形式输出集合的键和值 之格式.
BufferedOutputStream bufferedOutputStreamObj = new BufferedOutputStream(new FileOutputStream("\\1212.txt"));
propertiesObj.setProperty("121", "221212");
propertiesObj.store(bufferedOutputStreamObj,null);
//以字节流之形式读取硬盘中的文件内容
BufferedInputStream bufferedInputStreamObj = new BufferedInputStream(new FileInputStream("\\1212.txt"));
propertiesObj.load(bufferedInputStreamObj);
}
将集合到文件,再文件到集合
public static void ReaderAndWriter() throws IOException {
//properties集合到文件 注意:文件内容其实是文本,软性规定其后缀为'properties'.
FileWriter fileWriterOfZiFu = new FileWriter("\\testC.properties");
Properties propertiesObj = new Properties();
propertiesObj.setProperty("中国", "5500");
propertiesObj.setProperty("日本", "1400");
propertiesObj.setProperty("美国", "300");
propertiesObj.setProperty("英国", "123223");
propertiesObj.setProperty("加拿大", "55003");
//store方法可以不通过遍历就可将自身集合的全部键和值写入到文件中,该方法的参数二'comments'一般默认为空.
//如果不存在该文件,则将自动创建,推荐文件后缀为'properties'.
propertiesObj.store(fileWriterOfZiFu, "我是备注信息哦!文件中将看到我");
//文件到properties集合.
FileReader fileReaderOfZifu = new FileReader("\\testC.properties");
Properties propertiesTwo = new Properties();
propertiesTwo.load(fileReaderOfZifu);
Set<String> a = propertiesTwo.stringPropertyNames();
for (String s : a) {
String key = s;
String vlue = propertiesTwo.getProperty(key);//getProperty 根据键获取值
System.out.println("键为:" + key + " 值为:" + vlue);
}
}
修改运维配置信息
//修改项目目录下druid.properties的配置文件,以模仿日常运维中的配置更改.
public static void sqlInfoOfIni() throws IOException {
//先读取配置信息
FileReader fileReaderObj = new FileReader("D:\\IdeaProjects\\senior-code\\day10-Senior-code\\druid.properties");
Properties propertiesObj = new Properties();
propertiesObj.load(fileReaderObj);
//遍历下看看有没有读取到
Set<String> info = propertiesObj.stringPropertyNames();
for (String s : info) {
String key = s;
System.out.println(key+"="+propertiesObj.getProperty(s));
}
//修改其中的密码为123456,键和值为:password=root
propertiesObj.setProperty("password", "123456");
//再将其全部的键和值信息输出到配置文件中.
FileWriter fileWriterObj = new FileWriter("D:\\IdeaProjects\\senior-code\\day10-Senior-code\\druid.properties");
propertiesObj.store(fileWriterObj, null);
}