Properties类的使用

Properties类

Properties类是Hashtable的直接子类,Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。
在这里插入图片描述
properties文件示例:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

key-value的形式存放,并且其都是字符串

常用方法

方法名作用
Object setProperty​(String key, String value)设置键值对到properties文件中
String getProperty​(String key)根据键获取值
void list​(PrintStream out)将Properties文件的数据打印到指定的字节输出流
void list​(PrintWriter out)将Properties文件的数据打印到指定的字符输出流
void load​(InputStream inStream)从字节流读取properties文件
void load​(Reader reader)从字符流读取properties文件
void store​(OutputStream out, String comments)加载配置字节流文件文件的键值对到properties对象
void store​(Writer writer, String comments)加载配置字符流文件文件的键值对到properties对象

1.读取properties文件

package com.gothic.properties_;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Properties01 {

    public static void main(String[] args) throws IOException {

        // 使用properties类来读取 xxx.properties文件
        Properties properties = new Properties();
        // 1.使用load方法将properties文件读入
        properties.load(new FileReader("src\\mysql.properties"));
        // 2.使用list方法将properties文件的内容显示到控制台
        properties.list(System.out);
        // 3.通过getProperty()方法 获取指定键名的属性值
        String userName = properties.getProperty("jdbc.username");
        System.out.println(userName);
        String password = properties.getProperty("jdbc.password");
        System.out.println(password);
    }
}

在这里插入图片描述

2.编写properties文件

package com.gothic.properties_;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Properties02 {

    public static void main(String[] args) throws IOException {
        // 使用porperties类来编写配置文件

        Properties properties = new Properties();
        // properties类在存储中文的时候,会转换成unicode编码
        properties.setProperty("username","张三");
        properties.setProperty("password","123456");

        // setProperty() 如果键已经存在,则为修改其对应的值,不存在则创建相应的key-value
        // 修改密码 123456 --> abcdefg
        properties.setProperty("password","abcdefg");

        // 使用 store()方法创建properties文件 store方法的第二个参数,用来设置注释
        properties.store(new FileWriter("src\\myStore.properties"),"我是注释");



    }
}

在这里插入图片描述
这里 注释的中文变成了unicode编码 \u6211\u662F\u6CE8\u91CA对应我是注释
在这里插入图片描述

3.setProperty方法的底层,哈希table

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<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);
        return null;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杀死一只知更鸟debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值