[Java基础] Properties类的基本操作和介绍

引言

Java中的.properties文件是一种配置文件,主要用于表达配置信息;通俗来说,存放的数据就像是Map中的key和value的对应关系一样;这样就可以通过键值对来对属性进行匹配,并且属性列表中每个键及其对应值都是一个字符串(String类型);

在Java中存在一个Properties类可以用来保存属性集,它可以从流中加载属性集;所以接下来我将会详细介绍一下如何使用Properties类以及其中的常用的方法;

Properties类和和常用方法基本介绍

Properties:
在这里插入图片描述

中文:
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。


英文:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.

这里简单列举几个常用的方法,接下来的操作我也将会用到这些方法;

1,String getProperty(String key)

简介:通过key获取value
在这里插入图片描述
在这里插入图片描述

2,void load(InputStream inStream/Reader reader)

简介:将文件内容读入内存
在这里插入图片描述
在这里插入图片描述

3,Object setProperty(String key, String value)

简介:设置(增加)新的键值对
在这里插入图片描述
在这里插入图片描述

4,void store(OutputStream out / Writer writer, String comments)

简介:将内存中对应内容写入文件
在这里插入图片描述
在这里插入图片描述

这几个方法是比较常用的,当然还有几个方法没有写,如果需要可以在API文档中查询;

接下来我将会用代码实现一些操作,在这里先把.properties文件放出来:
下面的代码操作都是基于这个文件;
在这里插入图片描述

读取操作

读取操作可以通过load方法读取文件,再通过getProperty获取value值;
下面我用一个代码示范:

package io;

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

// 实现对.properties 文件的读取
public class PropertiesBlog01 {
    // 读入.properties文件
    public void readProperties() throws IOException {
        Properties properties = new Properties();
        // 把文件的数据读入内存
        properties.load(new FileInputStream("blogtest.properties"));
        // 输出文件的内容
        System.out.println(properties.getProperty("name")); // 获取姓名
        System.out.println(properties.getProperty("age")); // 获取年龄
        System.out.println(properties.getProperty("gender")); // 获取性别
    }

    // main方法调用测试
    public static void main(String[] args) throws IOException {
        PropertiesBlog01 propertiesBlog01 = new PropertiesBlog01();
        propertiesBlog01.readProperties();
    }
}

输出结果:

jack
18
man

写入操作

写入操作就有点意思了,这里写入分为三种情况:

  1. 写入新数据覆盖之前的数据
  2. 写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
  3. 写入新数据不会覆盖之前的数据,且有重复的键值会覆盖

这三种情况一般按需求使用,但是我感觉最常用的还是第三种(只是个人观点);
下面我用代码示范一下,代码中已有详细注释;

package io;

import java.io.*;
import java.util.Properties;

// 实现对.properties 文件的写入
public class PropertiesBlog02 {
    // 写入.properties 文件
    public void writeProperties() throws IOException {
        Properties properties = new Properties();
        // 下面是三种写入情况

        // 1,写入新数据覆盖之前的数据
        // 设置文件增添内容
        properties.setProperty("IdNum", "123456789");
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
        // 输出结果:
        // 原文件内容:       写入新数据覆盖后为:
        // name=jack         #this is a comment
        // age=18            #Fri Nov 19 19:58:08 CST 2021
        // gender=man        IdNum=123456789

        // 2,写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
        // 设置文件增添内容
        properties.setProperty("IdNum", "repeat");
        properties.store(new FileOutputStream("blogtest.properties", true), "this is a comment");
        // 输出结果:
        // 原文件内容:                       写入新数据后为:
        // #this is a comment                #this is a comment
        // #Fri Nov 19 19:58:08 CST 2021     #Fri Nov 19 20:06:03 CST 2021
        // IdNum=123456789                   IdNum=123456789
        //                                   #this is a comment
        //                                   #Fri Nov 19 20:06:03 CST 2021
        //                                   IdNum=repeat

        // 3,写入新数据不会覆盖之前的数据,且有重复的键值会覆盖
        properties.load(new FileInputStream("blogtest.properties"));
        properties.setProperty("IdNum", "newIdNum");
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
        // 输出结果:
        // 原文件内容:                       写入新数据后为:
        // #this is a comment                #this is a comment
        // #Fri Nov 19 20:06:03 CST 2021     #Fri Nov 19 20:13:22 CST 2021
        // IdNum=123456789                   IdNum=newIdNum
        // #this is a comment
        // #Fri Nov 19 20:06:03 CST 2021
        // IdNum=repeat
    }

    public static void main(String[] args) throws IOException {
        PropertiesBlog02 propertiesBlog02 = new PropertiesBlog02();
        propertiesBlog02.writeProperties();
    }
}

如果不太理解就记住就好了,忘记了就看看,写多了说不定有一天就会理解了;

删除操作

这个操作调用的remove方法,这个有点意思,因为你在API文档中找不到Properties类有这个方法,那么就可以看一下源码:
在这里插入图片描述
可以发现它实际调用了别人的remove方法,那么是谁的呢?我们再往下看:
在这里插入图片描述
这个其实就是ConcurrentHashMap的方法,所以这个remove方法并没有记在Properties类中;

其实学会怎么使用就行了,这个使用也很简单,同样用代码示范一下:

package io;

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

// 实现对.properties 文件内容的删除
public class PropertiesBlog03 {
    public void deleteProperties() throws IOException {
        Properties properties = new Properties();
        // 将文件数据加载到内存中
        properties.load(new FileInputStream("blogtest.properties"));
        // 通过键值删除对应的数据
        properties.remove("gender"); // 删除性别
        //重置文件中数据
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
    }

    public static void main(String[] args) throws IOException {
        PropertiesBlog03 propertiesBlog03 = new PropertiesBlog03();
        propertiesBlog03.deleteProperties();
    }
}

运行后文件变为:

#this is a comment
#Fri Nov 19 21:22:00 CST 2021
name=jack
age=18

可以看到gender键值对以及删除了;

总结

这就是对Properties的一些基础介绍,可能在使用时还会遇到其他问题,可以在评论区和我交流,希望这篇文章能够给你带来帮助!!

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YXXYX

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

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

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

打赏作者

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

抵扣说明:

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

余额充值