• JavaSE反射前置之Properties类


    1.查看需求

    在这里插入图片描述

    在这里插入图片描述
    比如说
    1.我们需要将配置文件信息读取到程序中
    2.另外一种我们在程序中修改了信息再写回进配置文件中Properties中 我们很多东西是在配置文件中写的 而不是写死在程序 程序操作数据库DB1和DB2 需要用户名和密码的 如果写死在程序 切换到DB2那么意味着要修改源码程序的 也就是源码都得修改且得重新编译 一般程序运行在客户 客户无动手能力去修改源码的,且修改起来成本代价高 我们将相关信息写入配置文件 这样完成解耦

    管道流联想法

    这里是创建一个管道 从硬盘读取到内存 也就是缓冲区的流文件罢了
    这里load显示一个大read和getProperty完成对指定键值对读取共同完成就是我们的read罢了
    store才是write

    在这里插入图片描述
    这里是左侧的管道流进行读取

    一.传统方式

    1. 读文件

    package com.ReviewProperties.java;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    //传统方法
    public class Properties01 {
        public static void main(String[] args) throws IOException {
            //读取mysql.properties文件 并获得ip
            BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\main\\java\\com\\ReviewProperties\\mysql.properties"));
            String line="";
            while ((line=bufferedReader.readLine())!=null){ //循环读取人
                String[] split=line.split("=");
                //如果我们要求指定的ip值
                if("ip".equals(split[0])) {
                    System.out.println(split[0] + "值是:" + split[1]);
                }
            };
            bufferedReader.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ip值是:192.168.100
    这里我们可以看到我们得自己去书写过滤条件equal方法 很麻烦。每次获取新需求我们得自己写过滤条件 所以我们可以通过Properties方便实现这个方法

    2.Properties类读文件

    Properties类介绍
    在这里插入图片描述
    Properties专门读写文件集合的类 不能乱写 只能写成
    1.键=值 这种形式 2.默认类型为String类型
    在这里插入图片描述
    对于setProperty 如果没有这个键值对 就相当于默认添加了这个键值对 对于store 方法 如果这个键值对已经在配置文件中相当于覆盖

    //properties是中间缓冲区
    //1)专门用于读写配置文件的集合类
    //  配置文件的格式:
    //    键=值
    //    键=值
    //2)注意:键值对不需要有空格,值不需要用引号一起来。默认类型是String
    //3)Properties的常见方法
    //        1.load:加载配置文件的键值对到Properties对象
    //        2.list:将数据显示到指定设备/流对象
    //        3. getProperty(key):根据键获取值
    //        4.setProperty(key,value)设置键值对到Properties对象 如果没有key就新创建
    //        5. store:将Properties中的键值对存储到配置文件相当于创建一个文件如果已经存在就覆盖,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
    /*应用案例
            I.使用Properties类完成对mysql..properties的读取,看老师代码演示
            2.使用Properties类添加key-val到新文件mysql2.properties中
            3.使用Properties类完成对mysql..properties的读取,并修改某个key-val*/
    public class Properties02 {
        public static void main(String[] args) throws IOException {
            //1.创建Properties对象
            Properties properties1=new Properties();
            //2。加载指定配臀文件
            properties1.load(new FileReader("src\\main\\java\\com\\ReviewProperties\\mysql.properties"));
            //3.把k-v显示到控制台
            properties1.list(System.out);
            //4.根据key 获取对应的值
            String user=properties1.getProperty("user");
            String pwd=properties1.getProperty("pwd");
            System.out.println("用户名"+user);
            System.out.println("密码"+pwd);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    – listing properties –
    user=root
    pwd=12345
    ip=192.168.100
    用户名root
    密码12345

        public static void main(String[] args) throws IOException {
            //使用Properties类创建配置文件 修改配置文件内容
            Properties properties=new Properties();
            //创建 现在简直对在内存里里面
            //1.如果该文件没有key就是创建
            //2.如果该文件有key,就是修改
            /**
             * Properties父类是Hashtable 底层就是Hashtable 核心方法
             *  public synchronized V put(K key, V value) {
             *         // Make sure the value is not null
             *         if (value == null) {
             *             throw new NullPointerException();
             *         }
             *
             *         // Makes sure the key is not already in the hashtable.
             *         Entry tab[] = table;
             *         int hash = key.hashCode();
             *         int index = (hash & 0x7FFFFFFF) % tab.length;
             *         @SuppressWarnings("unchecked")
             *         Entry entry = (Entry)tab[index];
             *         for(; entry != null ; entry = entry.next) {
             *             if ((entry.hash == hash) && entry.key.equals(key)) {
             *                 V old = entry.value;
             *                 entry.value = value; //如果key存在就替换
             *                 return old;
             *             }
             *         }
             *
             *         addEntry(hash, key, value, index); 如果是新k 就add
             *         return null;
             *     }
             */
            properties.setProperty("charset","utf8");
            properties.setProperty("user","tom汤姆"); //注意保存的是 中文的unicode码
            properties.setProperty("pwd", "9999");
    
            //将k-v存储文件中即可
            properties.store(new FileOutputStream("src\\main\\java\\com\\ReviewProperties\\mysql2.properties"), "hello world");
            //第二个参数仅仅是一个注释
    
            System.out.println("保存配置文件成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3.Properties修改文件

    package com.ReviewProperties.java;
    
    //        1.load:加载配置文件的键值对到Properties对象
    //        2.list:将数据显示到指定设备/流对象
    //        3. getProperty(key):根据键获取值
    //        4.setProperty(key,value)设置键值对到Properties对象 如果没有key就新创建
    //        5. store:将Properties中的键值对存储到配置文件相当于创建一个文件如果已经存在就覆盖,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Properties03 {
        public static void main(String[] args) throws IOException {
            //使用Properties类创建配置文件 修改配置文件内容
            Properties properties=new Properties();
            //创建 现在简直对在内存里里面
            //1.如果该文件没有key就是创建
            //2.如果该文件有key,就是修改
            /**
             * Properties父类是Hashtable 底层就是Hashtable 核心方法
             *  public synchronized V put(K key, V value) {
             *         // Make sure the value is not null
             *         if (value == null) {
             *             throw new NullPointerException();
             *         }
             *
             *         // Makes sure the key is not already in the hashtable.
             *         Entry tab[] = table;
             *         int hash = key.hashCode();
             *         int index = (hash & 0x7FFFFFFF) % tab.length;
             *         @SuppressWarnings("unchecked")
             *         Entry entry = (Entry)tab[index];
             *         for(; entry != null ; entry = entry.next) {
             *             if ((entry.hash == hash) && entry.key.equals(key)) {
             *                 V old = entry.value;
             *                 entry.value = value; //如果key存在就替换
             *                 return old;
             *             }
             *         }
             *
             *         addEntry(hash, key, value, index); 如果是新k 就add
             *         return null;
             *     }
             */
            properties.setProperty("charset","utf8");
            properties.setProperty("user","tom汤姆"); //注意保存的是 中文的unicode码
            properties.setProperty("pwd", "9999");
    
            //将k-v存储文件中即可
            properties.store(new FileOutputStream("src\\main\\java\\com\\ReviewProperties\\mysql2.properties"), "hello world");
            //第二个参数仅仅是一个注释
    
            System.out.println("保存配置文件成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    输出结果
    对于这个store方法的第二个参数 他会显示在mysql2.properties文件的最上方
    在这里插入图片描述

  • 相关阅读:
    JUC-原子操作类之18罗汉增强
    20个实用的 TypeScript 单行代码汇总
    <十>关于菱形继承
    线程池的使用(结合Future/Callable使用)
    C语言和Rust语言的互相调用(2)(Rust调用C)
    SLAM中去除动态物体的部分方法(主要是视觉SLAM)
    RocketMQ中Broker接收消息流程代码解析
    【JavaWeb】Servlet系列——HttpServletRequest接口详解
    学习 Mybatis
    11.15 - 每日一题 - 408
  • 原文地址:https://blog.csdn.net/lpblog/article/details/127730923