Java中Properties类,为什么用Properties类

要求:配置一个mysql1.properties的文件,其中包括

ip=192.168.10.13
user=root
pwd=111111

传统方法:

代码:

public class Properties1 {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split("=");
            //if(split[0].equals("ip"))//只读ip
            System.out.println(split[0] + "是:" + split[1]);
        }
    }

}

使用Properties类:

代码:

public class Properties1 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        properties.list(System.out);
    }
}

Properties类常用方法:

代码:

public class PropertiesMethods {
    public static void main(String[] args) throws IOException {
        //使用 Properties 类来读取 mysql.properties 文件
        //1. 创建 Properties 对象
        Properties properties = new Properties();
        //2. 加载指定配置文件
        properties.load(new FileReader("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties1\\mysql1.properties"));
        //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);
    }
}

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

代码:

public class Properties2 {
    public static void main(String[] args) throws IOException {
        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<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;
        }
        */

        properties.setProperty("ip", "192.168.10.13");
        properties.setProperty("user", "root");//注意保存时,是中文的 unicode 码值
        properties.setProperty("pwd", "111111");
        properties.store(new FileWriter("src\\main\\java\\com\\example\\retcode\\test\\file\\properties\\properties2\\mysql2.properties"), null);//null表示没有注解

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值