java基础-IO流对象之Properties集合

                java基础-IO流对象之Properties集合

                                  作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Properties集合的特点

  Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载,属性列表中每个键即及其对应值都是一个字符串。总结其显而易见的特点如下:

    1>.Hashtable的子类,map集合中的方法都有可以用;

    2>.该集合没有泛型,键值都是字符串;

    3>.它是一个可以持久化的属性集,键值可以存储到集合中,也可以存储到持久化的设备(硬盘,U盘,光盘)上。键值的来源也可以是持久化的设备。

    4>.有和流计数相结合的方法如:“load(InputStream inStream)”,“load(Reader reader) ”,“store(OutputStream out, String comments) ”,“store(Writer writer, String comments) ”。

二.Properties集合常用方法

1>.Properties集合存储键值对

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.util.Properties;
10 import java.util.Set;
11 
12 public class PropertiesDemo {
13     public static void main(String[] args) {
14         Properties p = new Properties();
15         //1>.使用setProperty方法存储键值对,其底层调用的是父类(Hashtable)的put方法,可以通过源码查看。
16         p.setProperty("Name", "yinzhengjie");
17         p.setProperty("Age", "18");
18         p.setProperty("Hobby", "Cosplay");
19         
20         System.out.println(p);
21         //2>.通过键获取值,如果给定的键值不存在就返回null。
22         String value = p.getProperty("Name1");
23         System.out.println(value);
24         
25         //3>.通过stringPropertyNames方法遍历Properties集合,该方法是将集合中的键存储到Set集合,类似于Map接口的方法keySet.
26         Set<String> set = p.stringPropertyNames();
27         for (String key : set) {
28             System.out.println(key+"==="+p.getProperty(key));
29         }
30         
31     }
32 }
33 
34 
35 /*
36 以上代码执行结果如下:
37 {Hobby=Cosplay, Age=18, Name=yinzhengjie}
38 null
39 Hobby===Cosplay
40 Age===18
41 Name===yinzhengjie
42 */

2>.(load方法)流对象读取文件中的键值对,保存到集合

 

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.util.Properties;
12 
13 public class PropertiesDemo {
14     public static void main(String[] args) throws IOException {
15         Properties p = new Properties();
16         //创建字符流对象,用于去读文件
17         FileReader fr = new FileReader("yinzhengjie.properties");
18         //调用集合的方法load,传递字符输入流,把文件的内容都加载到p对象中。
19         p.load(fr);
20         //加载完毕后,fr流对象就没有任何的利用价值了,关掉即可。
21         fr.close();
22         System.out.println(p);
23 
24     }
25 }
26 
27 /*
28 以上代码执行结果如下:
29 {name=yinzhengjie, age=18, [email protected]}
30 */

3>.(store方法)将集合中的键值对,写入文件中保存

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.util.Properties;
12 import java.util.Set;
13 
14 public class PropertiesDemo {
15     public static void main(String[] args) throws IOException {
16         Properties p = new Properties();
17         //1>.使用setProperty方法存储键值对,其底层调用的是父类(Hashtable)的put方法,可以通过源码查看。
18         p.setProperty("Name", "yinzhengjie");
19         p.setProperty("Age", "18");
20         p.setProperty("Hobby", "Cosplay");
21         
22         FileWriter fw = new FileWriter("yinzhengjie.properties");
23         //将键值对写入文件,使用集合的方法store传递字符输出流。
24         p.store(fw, "Add by yinzhengjie");    //注释信息不建议写中文,因为它会默认转换成Unicode编码,不易阅读!
25         
26     }
27 }

  执行之后,会生成新的文件,内容如下:

三.小试牛刀

  游戏试玩次数限制,定义一个文件,内容为:“times=5”,该参数表示玩家可以体验游戏的次数,当其值为0时实用结束。

1 #Sat May 05 17:38:45 GMT+08:00 2018
2 times=5
yinzhengjie.properties 文件内容
 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note6;
 8 
 9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.util.Properties;
12 
13 /*
14  * Properties不属于io流体系,java.util包下.
15  *     作为Hashtable的子类使用
16  * 
17  */
18 public class PropertiesDemo {
19 
20     public static void main(String[] args) throws Exception {
21         Properties p = new Properties();
22         p.load(new FileInputStream("yinzhengjie.properties"));
23         
24         String times = p.getProperty("times");
25         int i = Integer.parseInt(times);
26         
27         if(i <= 0){
28             System.out.println("试用结束");
29         }else{
30             System.out.println("当前是第 : " + (5 - i + 1) + "次试用,还剩: " + (i - 1) +" 次");
31             play();
32             p.setProperty("times", i - 1 + "");
33             p.store(new FileOutputStream("yinzhengjie.properties"), null);
34         }
35     }
36 
37     private static void play() {
38         System.out.println("正在玩游戏...");
39     }
40 
41 }

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/8983166.html
今日推荐