详细讲解Java中的Properties类

前言

使用Properties出现中文乱码可看我这篇文章:properties出现中文乱码解决方法(万能)

1. 基本知识

Properties 类是 Java 中用于处理配置文件的工具类,它继承自 Hashtable 类,实现了 Map 接口。

  • 主要用于读取和写入属性文件,以键值对的形式存储数据。
  • 配置文件通常以 .properties 为扩展名,其中每一行表示一个属性或配置项。

使用该类可以更好的处理文件:

  • 配置文件管理: 主要用于读取和保存应用程序的配置信息,例如数据库连接信息、用户设置等。
  • 国际化: 用于加载不同语言的资源文件,方便国际化应用程序。
  • 持久化: 可以将配置信息持久化到文件,以便下次程序启动时重新加载。

对于其方法可查看其API接口:Properties API接口

构造方法如下:

方法表述
Properties()创建一个没有默认值的空属性列表。
Properties​(int initialCapacity)创建一个没有默认值的空属性列表,并且初始大小容纳指定数量的元素,而无需动态调整大小。
Properties​(Properties defaults)创建具有指定默认值的空属性列表。

常用的方法如下:

返回类型方法表述
StringgetProperty​(String key)
getProperty​(String key, String defaultValue)
在此属性列表中搜索具有指定键的属性。
voidlist​(PrintStream out)
list​(PrintWriter out)
将此属性列表打印到指定的输出流。
voidload​(InputStream inStream)从输入字节流中读取属性列表(键和元素对)。
voidload​(Reader reader)以简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
ObjectsetProperty​(String key, String value)调用 Hashtable方法 put 。
voidstore​(OutputStream out, String comments)将此 Properties表中的此属性列表(键和元素对)以适合使用 load(InputStream)方法加载到 Properties表的格式写入输出流。
voidstore​(Writer writer, String comments)将此 Properties表中的此属性列表(键和元素对)以适合使用 load(Reader)方法的格式写入输出字符流。

2. 代码示例

当使用 Properties 类时,你可以使用上述方法来读取和写入属性。以下是这些方法的一些简单的代码示例:

1. getProperty(String key)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 获取属性值
    String value = properties.getProperty("key");
    System.out.println("Value for key 'key': " + value);
} catch (IOException e) {
    e.printStackTrace();
}

2. getProperty(String key, String defaultValue)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 获取属性值,如果不存在则使用默认值
    String value = properties.getProperty("nonexistentKey", "default");
    System.out.println("Value for key 'nonexistentKey': " + value);
} catch (IOException e) {
    e.printStackTrace();
}

3. list(PrintStream out) / list(PrintWriter out)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 打印属性列表到控制台
    properties.list(System.out);
} catch (IOException e) {
    e.printStackTrace();
}

4. load(InputStream inStream)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    // 从输入流中读取属性列表
    properties.load(input);

    // 遍历所有键值对
    for (String key : properties.stringPropertyNames()) {
        String value = properties.getProperty(key);
        System.out.println(key + ": " + value);
    }
} catch (IOException e) {
    e.printStackTrace();
}

5. store(OutputStream out, String comments)

Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");

try (OutputStream output = new FileOutputStream("output.properties")) {
    // 将属性列表写入输出流
    properties.store(output, "Comments for the output file");
} catch (IOException e) {
    e.printStackTrace();
}

6. store(Writer writer, String comments)

Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");

try (Writer writer = new FileWriter("output.properties")) {
    // 将属性列表写入输出字符流
    properties.store(writer, "Comments for the output file");
} catch (IOException e) {
    e.printStackTrace();
}

3. Demo

上述的API方法可适当选择,完整的Demo可充分了解这个类的整体逻辑:

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

public class PropertiesDemo {

    public static void main(String[] args) {
        // 创建 Properties 对象
        Properties properties = new Properties();

        // 设置属性值
        properties.setProperty("username", "码农研究僧");
        properties.setProperty("password", "123456789");
        properties.setProperty("server", "https://blog.csdn.net/weixin_47872288");

        // 将属性列表写入输出流
        try (OutputStream output = new FileOutputStream("config.properties")) {
            properties.store(output, "Sample Configuration");
            System.out.println("Properties written to config.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 从输入流中读取属性列表
        try (InputStream input = new FileInputStream("config.properties")) {
            properties.load(input);

            // 获取属性值
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            String server = properties.getProperty("server");

            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
            System.out.println("Server: " + server);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 打印属性列表到控制台
        /**
         * -- listing properties --
         * password=123456789
         * server=https://blog.csdn.net/weixin_47872288
         * username=码农研究僧
         */
        properties.list(System.out);
    }
}

终端输出结果如下:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码农研究僧

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

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

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

打赏作者

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

抵扣说明:

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

余额充值