Properties集合的特点
* A: Properties集合的特点
* a: Properties类介绍
* Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串
* b: 特点
* Hashtable的子类,map集合中的方法都可以用。
* 该集合没有泛型。键值都是字符串。
* 它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
* 有和流技术相结合的方法。
* c: 方法介绍
* load(InputStream inputStream) 把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
* load(Reader reader) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)
* store(OutputStream outputStream,String commonts) 把集合中的数据,保存到指定的流所对应的文件中,参数commonts代表对描述信息
* stroe(Writer writer,String comments) 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符
Properties集合存储键值对
* A: Properties集合存储键值对
* a: 方法介绍
* 集合对象Properties类,继承Hashtable,实现Map接口
* 可以和IO对象结合使用,实现数据的持久存储
* 使用Properties集合,存储键值对
* setProperty等同与Map接口中的put
* setProperty(String key, String value)
* 通过键获取值, getProperty(String key)
* b: 案例代码
public class PropertiesDemo {
public static void main(String[] args)throws IOException {
function_2();
}
/*
* 使用Properties集合,存储键值对
* setProperty等同与Map接口中的put
* setProperty(String key, String value)
* 通过键获取值, getProperty(String key)
*/
public static void function(){
Properties pro = new Properties();
pro.setProperty("a", "1");
pro.setProperty("b", "2");
pro.setProperty("c", "3");
System.out.println(pro);
String value = pro.getProperty("c");
System.out.println(value);
//方法stringPropertyNames,将集合中的键存储到Set集合,类似于Map接口的方法keySet
Set<String> set = pro.stringPropertyNames();
for(String key : set){
System.out.println(key+"..."+pro.getProperty(key));
}
}
}
Properties集合的方法load
* A: Properties集合的方法load
* a: 方法介绍
* Properties集合特有方法 load
* load(InputStream in)
* load(Reader r)
* 传递任意的字节或者字符输入流
* 流对象读取文件中的键值对,保存到集合
* b: 案例代码
public class PropertiesDemo {
public static void main(String[] args)throws IOException {
function_1();
}
/*
* Properties集合特有方法 load
* load(InputStream in)
* load(Reader r)
* 传递任意的字节或者字符输入流
* 流对象读取文件中的键值对,保存到集合
*/
public static void function_1()throws IOException{
Properties pro = new Properties();
FileReader fr = new FileReader("c:\\pro.properties");
//调用集合的方法load,传递字符输入流
pro.load(fr);
fr.close();
System.out.println(pro);
}
}
Properties集合的方法store
* A: Properties集合的方法store
* a: 方法介绍
* Properties集合的特有方法store
* store(OutputStream out)
* store(Writer w)
* 接收所有的字节或者字符的输出流,将集合中的键值对,写回文件中保存
* b: 案例代码
public class PropertiesDemo {
public static void main(String[] args)throws IOException {
function_2();
}
/*
* Properties集合的特有方法store
* store(OutputStream out)
* store(Writer w)
* 接收所有的字节或者字符的输出流,将集合中的键值对,写回文件中保存
*/
public static void function_2()throws IOException{
Properties pro = new Properties();
pro.setProperty("name", "zhangsan");
pro.setProperty("age", "31");
pro.setProperty("email", "123456789@163.com");
FileWriter fw = new FileWriter("c:\\pro.properties");
//键值对,存回文件,使用集合的方法store传递字符输出流
pro.store(fw, "");
fw.close();
}
}
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)