javaday50_IO流_Properties学习与应用场景

一.Properties基础方法学习

package java_Studing_day50_IO流10;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;


public class propertiesStudy {
/********************
 *  Map
 *     |--Hashtable
 *        |--Properties
 * 
 * Properties集合:
 * 特点:
 *   1.集合中的建和值都是字符串类型
 *   2.集合中的数据可以保存到流中,或者从流中获取
 *   
 *   
 *   通常该集合用于操作以键值对形式存在的配置文件

 * 
 * 
 * 
 * 
 * 
 * */
    
   以下为学习代码:
    
    
    
    public propertiesStudy() {
// TODO Auto-generated constructor stub
    }


    public static void main(String[] args) throws IOException {
//propertiesDemo();
//propertiesDemo2();
//propertiesDemo3();
//propertiesDemo4();
//myLoad();
         modify();
    }


// TODO Auto-generated method stub

  
/************************修改配置表中的信息 ***********************************/

   //1.修改已有的配置表中的信息
    /*①.读取这个文件
     * ②并将这个文件中的键值数据存储到集合中
     * ③再通过集合对数据进行修改

     * ④再通过流将修改后的数据存储到文件中

     * 
     * 
     * */
    //读取这个文件
private static void modify() throws IOException { 
    File file=new File("F:\\PersistentConfigurationFile.txt");
    if(file.exists());
    BufferedReader bfr=new BufferedReader (new FileReader("F:\\PersistentConfigurationFile.txt"));
  
    Properties prop=new Properties();//新建配置表
   
   prop.load(bfr);//加载本地文件到配置表
   
   prop.setProperty("Renekton", "6300");//修改配置表信息
   
   BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));
   prop.store(bfw, "name&sale2ndModify"); //存储到本地文件中,完成更新
    
   bfr.close();
   bfw.close();
}  

 /************模拟Load方法的实现过程*******************************************/

public static void myLoad() throws IOException{
    Properties prop=new Properties();
    BufferedReader bfr=new BufferedReader(new FileReader("F:\\PersistentConfigurationFile.txt"));//关联配置文件
    
    String line=null;
    while((line=bfr.readLine())!=null){ 
if(line.startsWith("#")) //如果不是#开头,则继续,否则退出本次循环
   continue;
String []arr=line.split("=");//将第一行的数据用=分割,分别保存在String[]的0位和1位
System.out.println(arr[0]+"-------------"+arr[1]); //打印这个数组
    }
    bfr.close();
}    
    
    
    

/****************从文件中加载配置表,Load方法*******************************/    

public  static void propertiesDemo4() throws IOException {
// 集合中的数据来自一个文件中
        //注意:必须保证该文件中的数据是键值对
        //需要使用到读取流
    Properties prop=new Properties();
    FileInputStream fis=new FileInputStream("F:\\PersistentConfigurationFile.txt");
    BufferedInputStream bfr=new BufferedInputStream(fis);
    
    prop.load(bfr);//load和流关联
    prop.list(System.out);
    
    }


/***************演示properties持久化,Store方法  *******************/

public static void propertiesDemo3() throws IOException {
  //创建一个properties集合
    Properties prop=new Properties();
    
    //存储元素
    prop.setProperty("VN", "4800");
    prop.setProperty("Jinx", "6300");
    prop.setProperty("Renekton", "4800");
    prop.setProperty("Ashe", "450");
    prop.setProperty("Poppy", "3150");
    
   //想要将这些集合中的字符串键值信息持久化存储到文件中
    //需要关联输出流
   BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));
   
   prop.store(bfw, "HeroName+Sale");//Store和流关联
   bfw.close();




}


/***************演示properties集合和流对象相结合的功能*******************/    

 public static void propertiesDemo2(){
   //创建一个properties集合
     Properties prop=new Properties();
     
     //存储元素
     prop.setProperty("VN", "4800");
     prop.setProperty("Jinx", "6300");
     prop.setProperty("Renekton", "4800");
     prop.setProperty("Ashe", "450");
     prop.setProperty("Poppy", "3150");
     prop=System.getProperties();
     //list方法,将配置列表中的信息输出到一个输出流,这个在调试时很有用
     prop.list(System.out);//打印到控制台
 }   
    

/****************properties集合的创建/增/删/改/查/******************/

public static void propertiesDemo(){
    //创建一个properties集合
    Properties prop=new Properties();
    
    //存储元素
    prop.setProperty("VN", "4800");
    prop.setProperty("Jinx", "6300");
    prop.setProperty("Renekton", "4800");
    prop.setProperty("Ashe", "450");
    prop.setProperty("Poppy", "3150");
   //删除元素
    prop.remove("renekton");
    //修改元素
    prop.setProperty("VN", "6300");
    //取出所有元素
    Set<String> names=prop.stringPropertyNames();//返回一个set集合
    for(String name:names){
String value=prop.getProperty(name);
System.out.println(name+"---------"+value);
    }
    
}
    
}

2.Properties集合的练习与总结

package java_Studing_day50_IO流10;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


/*******************************************************************************
 * 定义功能:
 *     获取一个程序的运行次数,如果超过5次,给出使用次数已到,请注册的提示,并不要再运行程序
 * 思路:
 * 1.应该有计数器,每次程序启动计数一次,并且是在原有的次数上进行计数
 * 
 * 2.计数器就是一个变量,突然冒出一个想法?>>程序启动时进行计数,计数器就必须存在内存中进行计算,
 *  可是程序一结束计数消失,再次启动该程序,计数器会被初始化,白记了,解决方法:存到硬盘文件
 * 
 * 3.如何使用计数器>
 * 首先,程序启动时,应该首先读取用于这个记录计数器信息的配置文件
 * 获取上一次计数次数
 * 其次,对该次数进行自增,并将自增后的次数重新存储到配置文件中
 * 
 * 4.文件中的信息该如何进行存储和体现呢??
 *  直接存储次数就可以,但是不明确该数值的意义,所以取名字很重要
 *  这就有了名字和值得对应,所以可以使用键值对
 *  可是映射关系map集合搞定,有需要读取硬盘上的数据,所以 Map+Io=Properties,搞定!
 * 
 * 键值对可以表示配置信息,标签也可以表示;
 * 就是尖括号将对象封装
 * <Persons>
 *  <Person id="007">
 *    <name>jorian</name>
 *    <age>20</age>
 *    <ID>10222</ID>
 *  </Person>
 * 
 *   <person id="008">
 *    <name>jiaoyan</name>
 *    <age>22</age>
 *    <ID>10223</ID>
 *   </person>
 * 
 * </Persons>
 * 
 * 
 * 总结:
 * 我们之所以打开软件还是上一次的一些个性化设置,就是因为将更改后的配置信息保存到了本地,所以此方法可以用来维护程序的配置信息

 * 
 * 
 * 
 * 
 * 
 **********************************************************************************/ 

 


public class PropertiesPractice {






    public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
         getAppCount();//此方法在程序运行前运行,可以监控程序运行的次数
    }


    public static void getAppCount() throws IOException {
//将配置文件封装成File对象
File config=new File("F:\\Appcounter.properties");

if(config.exists()){
config.createNewFile();
}
FileInputStream fis=new FileInputStream(config);

Properties prop=new Properties();
//加载本地配置表 
prop.load(fis); 
  
 //从集合中通过键获取次数,得到String类型数据
  String value=prop.getProperty("time");
  //创建计数器 
  int count=0;
  //如果
  if(value!=null){
count=Integer.parseInt(value);//将String类型的次数信息转Int型,然后传给计数器

if(count>=5){
   System.out.println("免费试用次数已到,请注册!谢谢!"); 
   throw new RuntimeException("使用次数已到,给钱!") ;
}
  }
   count++;
   
   //将改变后的次数从新存储到集合中
   prop.setProperty("time", count+"");
   
   FileOutputStream fos=new FileOutputStream(config);
   //更新,存储到本地
   prop.store(fos, "counterModify");
  
   fis.close();
   fos.close();
  


   }  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值