【JavaSE】Properties类,交互文件

一、先看一个需求

如下一个配置文件 mysql.properties
ip = 192.168.0.13
user = root
pwd = 12345

请问编程呢个读取 ip、user和pwd的值是多少

第一种:用传统的字节流读取
第二种:使用Properties类可以方便实现

在这里插入图片描述

第一种:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Properties1 {
    public static void main(String[] args) {

        //读取mysql.properties 文件,并得到ip, user 和 pwd
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\mysql.properties"));
            String line = "";
            while ((line = br.readLine()) != null){
                String[] s = line.split("=");
                //如果我们要求指定的ip值
                if("ip".equals(s[0])) {
                    System.out.println(s[0] + "值是: " + s[1]);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

二、类的基本介绍

1)专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值

2)注意:键值对不需要有空格,值不需要用引号引来。默认类型为String

3)Properties的常见的方法

  • load:加载配置文件的键值对到Properties对象
  • list:将数据显示到指定设备
  • getProperty(key):根据键获取值
  • setProperty(key,value):设置键值对到Properties对象
  • store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码

三、应用案例

1)使用Properties类完成对MySQL.properties的读取
2)使用Properties类添加Key-val 到新文件mysql2.properties中
3)使用Properties类完成对 mysql2.properties 的读取,并修改某个Key-val

1)使用Properties类完成对MySQL.properties的读取

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

public class Properties02 {
    public static void main(String[] args) {

        //使用Properties 类来读取mysql.properties 文件

        //1. 创建Properties 对象
        Properties properties= new Properties();
        //2. 加载指定配置文件
        try {
            properties.load(new FileReader("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\mysql.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        //3. 把k-v显示控制台
        properties.list(System.out);
        //4. 根据key 获取对应的值
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println("用户名=" + user);
        System.out.println("密码是=" + pwd);
    }
}

2)使用Properties 类来创建 配置文件, 修改配置文件内容

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

public class Properties03 {
    public static void main(String[] args) {

        //使用Properties 类来创建 配置文件, 修改配置文件内容
        Properties properties = new Properties();

        //创建
        //1.如果该文件没有key 就是创建
        //2.如果该文件有key ,就是修改

        properties.setProperty("charset", "utf8");
        properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode码值
        properties.setProperty("pwd", "888888");

        //将k-v 存储文件中即可
        try {
            properties.store(new FileOutputStream("D:\\JavaProjects\\b站草稿\\IO控制流\\src\\mysql.properties2"), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("保存配置文件成功~");
    }
}

三、底层机制

底层其实是哈希算法

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<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;//如果key 存在,就替换
                        return old;
                    }
                }

                addEntry(hash, key, value, index);//如果是新k, 就addEntry
                return null;
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值