Java基础--IO流之Properties

凌风博客原创作品。转载请注明出处:http://blog.csdn.net/q549130180/article/details/45333441


Properties是hashtable的子类

也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串


是集合中和IO技术相结合的集合容器


该对象的特点:可以用于键值对形式的配置文件。


那么在加载数据时,需要数据有固定格式:键=值


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. import java.util.*;  
  3. class PropertiesDemo_1  
  4. {  
  5.     public static void main(String[] args) throws IOException  
  6.     {  
  7.         loadDemo();  
  8.     }  
  9.   
  10.       
  11.     public static void loadDemo() throws IOException  
  12.     {  
  13.         Properties prop = new Properties();  
  14.         FileInputStream fis = new FileInputStream("info.txt");  
  15.   
  16.         //将流中的数据加载进集合  
  17.         prop.load(fis);  
  18.           
  19.         //改变键的值  
  20.         prop.setProperty("wangwu","39");  
  21.   
  22.         FileOutputStream fos = new FileOutputStream("info.txt");  
  23.   
  24.         prop.store(fos,"zhushi");//将改变的键的值存储到文件中,存储时需要输出流对象  
  25.   
  26.         //System.out.println(prop);  
  27.         prop.list(System.out);  
  28.   
  29.         fos.close();  
  30.         fis.close();  
  31.     }  
  32. }  



演示:如何将流中的数据存储到集合中
想要将info.txt中键值数据存储到集合中进行操作


1.用一个流和info.txt文件关联
2.读取一行数据,将该行数据用"="进行切割
3.等号左边作为键,右边作为值,存入到Properties集合中即可

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static void method_1() throws IOException  
  2.     {  
  3.         BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));  
  4.   
  5.         String line = null;  
  6.         Properties prop = new Properties();  
  7.   
  8.         while ((line=bufr.readLine())!=null)  
  9.         {  
  10.             String[] arr = line.split("=");  
  11.             prop.setProperty(arr[0],arr[1]);  
  12.         }  
  13.         bufr.close();  
  14.   
  15.         System.out.println(prop);  
  16.     }  
  17.   
  18.     //设置和获取元素  
  19.     public static void setAndGet()  
  20.     {  
  21.         Properties prop = new Properties();  
  22.   
  23.         prop.setProperty("zhangsan","30");  
  24.         prop.setProperty("lisi","39");  
  25.   
  26.         String value = prop.getProperty("lisi");  
  27.   
  28.         Set<String> names = prop.stringPropertyNames();  
  29.         for(String s : names)  
  30.         {  
  31.             System.out.println(s+":"+prop.getProperty(s));  
  32.         }  
  33.     }  

用于记录应用程序运行次数
如果使用次数已到,那么给出注册提示


很容易想到的是:计数器
可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增
可是随着该应用程序的退出,该计数器也在内存中消失了


下一次在启动该程序,又重新开始从0计数
这不是我们想要的


程序即使结束,该计数器的值也存在。
下次程序启动前会先加载该计数器的值并加1后在重新存储起来


所以要建立一个配置文件,用于记录该软件的使用次数


该配置文件使用键值对的形式
这样便于阅读数据,并操作数据


键值对数据时map集合
数据是以文件形式存在,使用io技术
那么map+io -->properties


配置文件可以实现应用程序数据的共享

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class RunCount_2  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         Properties prop = new Properties();  
  6.   
  7.         File file = new File("count.ini");  
  8.         fi(!file.exists())  
  9.             file.createNewFile();  
  10.   
  11.         FileInputStream fis = new FileInputStream(file0);  
  12.   
  13.         prop.load(fis);  
  14.   
  15.         int count = 0;  
  16.   
  17.         String value = prop.getProperty("time");  
  18.   
  19.         if (value!=null)  
  20.         {  
  21.             count = Integer.paraeInt(value);  
  22.             if (count>5)  
  23.             {  
  24.                 System.out.println("您好,使用次数以到,拿钱!");  
  25.                 return ;  
  26.             }  
  27.         }  
  28.         count++;  
  29.         prop.setProperties("time",count+"");  
  30.   
  31.         FileOutputStream fos = new FileOutputStream(file);  
  32.   
  33.         prop.store(fos,"");  
  34.   
  35.         fos.close();  
  36.         fis.close();  
  37.     }  
  38. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值