File类与常用IO流第七章——Properties集合
-
Properties概述
java.util.Properties extends Hashtable<k,v> implements Map<k,v>
Properties类表示了一个持久的属性集。
Properties可以保存在流中或从流中加载。
Properties集合是唯一一个和IO流相结合的集合:
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。
可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用。
属性列表中每个键值对及其对应值都是一个字符串。
Properties集合是一个双列集合,key和value默认都是字符串。
-
-
使用Properties集合存储数据,遍历取出Properties集合中的数据
Properties集合是一个Map集合,key和value默认都是字符串。
Properties集合中有一些操作字符串的特有方法
-
Object setProperty(String key,String value):调用Hashtable的方法put。
-
String getProperty(String key):用指定的键(key)在此属性列表中搜索属性(value)。相当于Map集合中的get(key)方法。
-
Set<String> stringPropertyNames():返回此属性列表中的键值,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。此方法相当于Map集合中的keySet方法。
1 Properties prop = new Properties(); 2 prop.setProperty("一班","68人"); 3 prop.setProperty("二班","78人"); 4 prop.setProperty("三班","88人"); 5 //prop.put(1,true);//并没有被保存进去 6 7 Set<String> stringSet = prop.stringPropertyNames(); 8 9 for (String key : stringSet) { 10 String value = prop.getProperty(key); 11 System.out.println(key+"="+value); 12 }
运行结果:prop.put(1,true);//并没有被保存进去
-
Properties集合中的方法store
使用这个方法可以将集合中的临时数据,持久化写入到硬盘中存储
-
void store(OutputStream out , String comments)
-
void store(Write write , String comments)
-
参数:
-
OutpueStream out :字节输出流,不能写中文
-
Write write :字符输出流,可以写中文
-
String comments :注释,用来解释说明保存的文件是做什么用的
-
comments的注意:不能使用中文,会产生乱码,默认Unicode编码格式。一般使用空字符串 " " ;1
-
使用步骤:
-
创建Properties集合,添加数据
-
创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
-
使用Properties集合中的方法store将集合中的临时数据,持久化写入到硬盘中存储
-
释放资源
-
匿名对象使用完毕会自动关闭其中申请的资源。
1 //1.创建Properties集合对象,添加数据 2 Properties prop = new Properties(); 3 prop.setProperty("赵丽颖","168"); 4 prop.setProperty("迪丽热巴","165"); 5 prop.setProperty("古力娜扎","160"); 6 7 //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 8 FileWriter fw = new FileWriter("09_IOAndProperties\\prop.txt"); 9 10 //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 11 prop.store(fw,"save data"); 12 13 //4.释放资源 14 fw.close();
1 //1.创建Properties集合对象,添加数据 2 Properties prop = new Properties(); 3 prop.setProperty("赵丽颖","168"); 4 prop.setProperty("迪丽热巴","165"); 5 prop.setProperty("古力娜扎","160"); 6 7 //2.&3.&4.匿名对象使用完会自动关闭其中申请的资源 8 prop.store(new FileWriter("09_IOAndProperties\\prop2.txt"),"");
-
-
-
Properties集合中的方法load
使用此方法,可以将硬盘中保存的文件(键值对),读取到集合中使用
-
void load(InputStream inStream)
-
void load(Reader reader)
-
参数:
-
InputStream inStream:字节输入流,不能读取含有中文的键值对
-
Reader reader:字符输入流,能读取含有中文的键值对
-
使用步骤:
-
创建Properties集合对象
-
使用Properties集合对象中的方法load读取保存键值对的文件
-
遍历Properties集合
-
注意:
-
存储键值对的文件中,键与值默认的连接符号可以使用 = 或空格
-
存储键值对的文件中,可以使用 # 进行注释,被注释的键值对不会再被读取
-
存储荐椎对的文件中,键与值的默认都是字符串,不用再加引号
-
代码:
1 //1.创建Properties集合对象 2 Properties prop = new Properties(); 3 //2.使用Properties集合对象中的方法load读取保存键值对的文件 4 prop.load(new FileReader("09_IOAndProperties\\prop.txt")); 5 //prop.load(new FileInputStream("09_IOAndProperties\\prop.txt")); 6 //3.遍历Properties集合 7 Set<String> set = prop.stringPropertyNames(); 8 for (String key : set) { 9 String value = prop.getProperty(key); 10 System.out.println(key+"="+value); 11 }
本文来自博客园,作者:水啾2,转载请注明原文链接:https://www.cnblogs.com/shuijiu/p/15020972.html

浙公网安备 33010602011771号