Properties集合与IO流
1 package com.itheima.demo07.Prop; 2 3 import java.io.FileOutputStream; 4 import java.io.FileReader; 5 import java.io.IOException; 6 import java.util.Properties; 7 import java.util.Set; 8 9 /* 10 java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v> 11 Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。 12 Properties集合是一个唯一和IO流相结合的集合 13 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 14 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用 15 16 属性列表中每个键及其对应值都是一个字符串。 17 Properties集合是一个双列集合,key和value默认都是字符串 18 */ 19 public class Demo01Properties { 20 public static void main(String[] args) throws IOException { 21 show03(); 22 } 23 24 /* 25 可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用 26 void load(InputStream inStream) 27 void load(Reader reader) 28 参数: 29 InputStream inStream:字节输入流,不能读取含有中文的键值对 30 Reader reader:字符输入流,能读取含有中文的键值对 31 使用步骤: 32 1.创建Properties集合对象 33 2.使用Properties集合对象中的方法load读取保存键值对的文件 34 3.遍历Properties集合 35 注意: 36 1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号) 37 2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取 38 3.存储键值对的文件中,键与值默认都是字符串,不用再加引号 39 */ 40 private static void show03() throws IOException { 41 //1.创建Properties集合对象 42 Properties prop = new Properties(); 43 //2.使用Properties集合对象中的方法load读取保存键值对的文件 44 prop.load(new FileReader("09_IOAndProperties\\prop.txt")); 45 //prop.load(new FileInputStream("09_IOAndProperties\\prop.txt")); 46 //3.遍历Properties集合 47 Set<String> set = prop.stringPropertyNames(); 48 for (String key : set) { 49 String value = prop.getProperty(key); 50 System.out.println(key+"="+value); 51 } 52 } 53 54 /* 55 可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 56 void store(OutputStream out, String comments) 57 void store(Writer writer, String comments) 58 参数: 59 OutputStream out:字节输出流,不能写入中文 60 Writer writer:字符输出流,可以写中文 61 String comments:注释,用来解释说明保存的文件是做什么用的 62 不能使用中文,会产生乱码,默认是Unicode编码 63 一般使用""空字符串 64 65 使用步骤: 66 1.创建Properties集合对象,添加数据 67 2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 68 3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 69 4.释放资源 70 */ 71 private static void show02() throws IOException { 72 //1.创建Properties集合对象,添加数据 73 Properties prop = new Properties(); 74 prop.setProperty("赵丽颖","168"); 75 prop.setProperty("迪丽热巴","165"); 76 prop.setProperty("古力娜扎","160"); 77 78 //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地 79 //FileWriter fw = new FileWriter("09_IOAndProperties\\prop.txt"); 80 81 //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 82 //prop.store(fw,"save data"); 83 84 //4.释放资源 85 //fw.close(); 86 87 prop.store(new FileOutputStream("09_IOAndProperties\\prop2.txt"),""); 88 } 89 90 /* 91 使用Properties集合存储数据,遍历取出Properties集合中的数据 92 Properties集合是一个双列集合,key和value默认都是字符串 93 Properties集合有一些操作字符串的特有方法 94 Object setProperty(String key, String value) 调用 Hashtable 的方法 put。 95 String getProperty(String key) 通过key找到value值,此方法相当于Map集合中的get(key)方法 96 Set<String> stringPropertyNames() 返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map集合中的keySet方法 97 */ 98 private static void show01() { 99 //创建Properties集合对象 100 Properties prop = new Properties(); 101 //使用setProperty往集合中添加数据 102 prop.setProperty("赵丽颖","168"); 103 prop.setProperty("迪丽热巴","165"); 104 prop.setProperty("古力娜扎","160"); 105 //prop.put(1,true); 106 107 //使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中 108 Set<String> set = prop.stringPropertyNames(); 109 110 //遍历Set集合,取出Properties集合的每一个键 111 for (String key : set) { 112 //使用getProperty方法通过key获取value 113 String value = prop.getProperty(key); 114 System.out.println(key+"="+value); 115 } 116 } 117 }