zoukankan      html  css  js  c++  java
  • Java中Properties类的学习总结

    学习目标:

    1、认识properties文件,理解其含义,会正确创建properties文件。

    2、会使用java.util.Properties类来操作properties文件。

    一、认识properties文件

    1、properties文件是一个文本文件

    2、properties文件的语法有两种,一种是注释,一种属性配置。

     注    释:前面加上#号

     属性配置:以“键=值”的方式书写一个属性的配置信息。

    3、properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“”表示。

    4、properties的属性配置键值前后的空格在解析时候会被忽略。

    5、properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键。

    例如,下面一个properties文件:

     

    二、解读java.util.Properties类

      在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properties结尾的文件和xml文件)。Properties类是Hashtable的子类。也就是说它具备Map集合的特点。Properties的构造函数有两个,一个不带参数,一个使用一个Properties对象作为参数。

    1、Properties类的层次结构

    java.lang.Object

      java.util.Dictionary<K,V>

          java.util.Hashtable<Object,Object>

              java.util.Properties

    从层次机构看,Properties类实现了Map接口,因为HashTable实现了Map接口,因此Properties类本质上是一种简单的Map容器。

    实际上,Properties类本身表示了对一种Map结构的操作。properties文件本身就表示了一个“键值对”的集合。因此,Properties类属于集合容器的家族,在使用前应该创建一个Properties的容器,实际上就是创建一个默认不带参数的Properties对象。以后通过别的方式给里面添加“键值对”。

    2、properties文件与Properties类的关系

    通过properties文件可以填充Properties类。

    也可以通过xml文件来填充Properties类。

    可以通过绝对路径方式加载Properties文件信息,也可以使用相对路径加载。

    三、实践

    1、以绝对方式加载properties文件信息。

    2、将Properties对象持久化到一个properties文件或者一个xml文件中。

    3、修改并持久化properties文件。

    测试的properties文件:

     

    测试类:

      1 /** 
      2 * Properties类测试 
      3 * User: 支胜勇 
      4 */ 
      5 public class Test4 { 
      6     
      7         public static void main(String args[]) throws IOException { 
      8             
      9             System.out.println("------------测试Properties-------------");
     10             
     11             Properties prop=new Properties();
     12             prop.put("姓名", "支胜勇");
     13             prop.put("爱好", "编程和旅游");
     14             prop.put("籍贯", "贵州毕节");
     15             
     16             createProperFile("E:\tt.properties",prop);
     17             
     18             readProperFile("E:\tt.properties");
     19             
     20             Map<String,String> map=new HashMap<String,String>();
     21             map.put("性别", "男");
     22             map.put("学历", "本科");
     23             modifyProperFile("E:\tt.properties",map);
     24         } 
     25 
     26         
     27         /***
     28          * 创建properties文件,并向其写入键值对,包括properties和xml格式
     29          * @param fileName
     30          * @param keyValues
     31          */
     32         public static void createProperFile(String fileName,Properties prop){
     33              System.out.println("------------创建Properties文件----------");
     34             //创建properties文件
     35              
     36             File properFile=new File(fileName.substring(0,fileName.lastIndexOf("."))+".properties");
     37             if(!properFile.exists()){
     38                 try {
     39                     properFile.createNewFile();
     40                 } catch (IOException e) {
     41                     // TODO Auto-generated catch block
     42                     System.err.print("创建文件失败!");
     43                     return;
     44                 }
     45             }
     46             
     47             try {
     48                 
     49                 FileWriter fw=new FileWriter(properFile);
     50                 prop.store(fw, "我是通过properties写进来的");
     51                 OutputStream fw1=new FileOutputStream(fileName.substring(0,fileName.lastIndexOf("."))+".xml");
     52                 prop.storeToXML(fw1, "我是通过properties写进来的");
     53                 fw1.close();
     54                 fw.close();
     55                 
     56                 System.out.println("------------创建Properties文件成功----------");
     57             } catch (IOException e) {
     58                 // TODO Auto-generated catch block
     59                 System.err.print("向properties文件写入数据失败!");
     60                 return;
     61             }
     62         }
     63         
     64         
     65         /***
     66          * 读取properties文件
     67          * @param properFileName
     68          * @return
     69          * @throws IOException
     70          */
     71         public static Properties readProperFile(String properFileName) throws IOException{
     72             
     73             File file =new File(properFileName);
     74              Properties prop=null;
     75             if(!file.exists()){
     76                 
     77                 System.out.print("文件不存在!");
     78                 return null;
     79             }
     80             
     81             try {
     82                 System.out.println("------读取properties文件-------");
     83                 FileReader fr=new FileReader(file);
     84                  //创建一个Properties容器 
     85                 prop = new Properties(); 
     86                 //从流中加载properties文件信息 
     87                 prop.load(fr); 
     88                 //循环输出配置信息 
     89                 for (Object key : prop.keySet()) { 
     90                         System.out.println(key + "=" + prop.get(key)); 
     91                 }
     92                 
     93                 
     94             } catch (FileNotFoundException e) {
     95                 // TODO Auto-generated catch block
     96                 System.out.print("文件不存在!");
     97             }
     98             
     99             return prop;
    100         }
    101         
    102         
    103         
    104         /***
    105          * 修改properties文件,并持久化保存
    106          * @param properFileName
    107          * @param map
    108          * @throws IOException
    109          */
    110         Public static void modifyProperFile(String properFileName,Map<String,String> map) throws IOException{
    111             
    112             Properties prop=new Properties();
    113             
    114             if(properFileName.endsWith(".xml")||properFileName.endsWith(".XML")){
    115                 
    116                 prop.loadFromXML(new FileInputStream(properFileName));
    117                 for(String key:map.keySet()){
    118                     prop.put(key, map.get(key));
    119                 }
    120                 OutputStream os=new FileOutputStream(properFileName); 
    121                 prop.storeToXML(os, "我已经被修了");
    122                 os.close();
    123             }else{
    124                 prop.load(new FileReader(properFileName));
    125                 for(String key:map.keySet()){
    126                     prop.put(key, map.get(key));
    127                 }
    128                 FileWriter fw=new FileWriter(properFileName);
    129                 prop.store(fw, "我已经被修改了");
    130                 fw.close();
    131             }
    132         }
    133 }

    运行结果图:

  • 相关阅读:
    笨方法学python中执行argv提示ValueError: not enough values to unpack (expected 4, got 1)
    VMware workstation安装
    Redis bigkey分析
    MySQL drop table 影响及过程
    MySQL 大表硬连接删除
    ES elasticsearch 各种查询
    ES elasticsearch 各种聚合
    ES elasticsearch 聚合统计
    ES elasticsearch 实现 count单字段,分组取前多少位,以地理位置中心进行统计
    MySQL行溢出、varchar最多能存多少字符
  • 原文地址:https://www.cnblogs.com/zhishengyong/p/3788467.html
Copyright © 2011-2022 走看看