perties类的操作
发布日期:2025-05-02 00:52:12 浏览次数:52 分类:精选文章

本文共 3407 字,大约阅读时间需要 11 分钟。

Java Properties类及其应用

最近在学习一些开源模拟器的源码,发现其中大量涉及Java Properties类的使用。虽然已经有一段时间没用Java了,但由于这些模拟器多用Java编写,加上部分地方用Python、Perl等脚本语言处理,导致不得不重新拾起Java知识。这篇文章将简单介绍Java Properties类的功能及其常见应用。

Java Properties类概述

Java中的Properties类(位于java.util.Properties包中)是一个重要的配置文件处理类,主要用于读取和存储键值对数据。它实际上是Hashtable的子类,继承了 Hashtable的基本功能。Properties类的主要特点在于支持“键=值”的配置文件格式,常用于外部化配置管理。

Properties类的主要方法

  • getProperty(String key)

    使用指定的键在属性列表中搜索对应的值。

  • load(InputStream inStream)

    从输入流中读取键值对,装载到Properties对象中。常用于读取外部的.properties文件。

  • setProperty(String key, String value)

    调用Hashtable的put方法,设置指定键的值。

  • store(OutputStream out, String comments)

    将当前Properties表中的键值对写入输出流。与load方法相反,用于生成可供load的配置文件。

  • clear()

    清除Properties表中的所有键值对。

  • Java读取Properties文件

    读取Properties文件的方法有多种,但最常用的是通过类资源加载或文件输入流读取。以下是两种常用方法:

  • 通过类资源加载

    使用Class.getResourceAsStream(String name)方法,适用于程序内部资源文件。

  • 通过文件输入流读取

    使用new BufferedInputStream(new FileInputStream(filepath)),适用于外部文件路径。

  • 相关实例

    为了加深对Properties类的理解,我们来看以下实例:

    1. 获取JVM系统属性

    import java.util.Properties;public class ReadJVM {    public static void main(String[] args) {        Properties pps = System.getProperties();        pps.list(System.out);    }}

    运行结果会显示JVM的系统属性列表。

    2. 读取自定义配置文件

    public class getProperties {    public static void main(String[] args) throws FileNotFoundException, IOException {        Properties pps = new Properties();        pps.load(new FileInputStream("Test.properties"));        Enumeration enum1 = pps.propertyNames();        while (enum1.hasMoreElements()) {            String strKey = (String) enum1.nextElement();            String strValue = pps.getProperty(strKey);            System.out.println(strKey + "=" + strValue);        }    }}

    3. 综合示例

    public class TestProperties {        // 根据Key读取Value    public static String GetValueByKey(String filePath, String key) {        Properties pps = new Properties();        try {            InputStream in = new BufferedInputStream(new FileInputStream(filePath));            pps.load(in);            String value = pps.getProperty(key);            System.out.println(key + " = " + value);            return value;        } catch (IOException e) {            e.printStackTrace();            return null;        }    }    // 读取Properties的全部信息    public static void GetAllProperties(String filePath) throws IOException {        Properties pps = new Properties();        InputStream in = new BufferedInputStream(new FileInputStream(filePath));        pps.load(in);        Enumeration en = pps.propertyNames();        while (en.hasMoreElements()) {            String strKey = (String) en.nextElement();            String strValue = pps.getProperty(strKey);            System.out.println(strKey + "=" + strValue);        }    }    // 写入Properties信息    public static void WriteProperties(String filePath, String pKey, String pValue) throws IOException {        Properties pps = new Properties();        InputStream in = new FileInputStream(filePath);        pps.load(in);        OutputStream out = new FileOutputStream(filePath);        pps.setProperty(pKey, pValue);        pps.store(out, "Update " + pKey + " name");    }    public static void main(String[] args) throws IOException {        // GetValueByKey("Test.properties", "name");        // GetAllProperties("Test.properties");        WriteProperties("Test.properties", "long", "212");    }}

    总结

    通过以上内容可以看出,Java的Properties类在配置文件管理中具有重要作用。无论是读取现有配置文件,还是存储新的键值对,Properties类都提供了便捷的API。熟练掌握Properties类的使用方法,对于日常开发工作非常有帮助。

    上一篇:petalinux环境安装和基本编译
    下一篇:perspective意思_2020年12月英语四级词汇讲解丨考点归纳:perspective

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2026年06月09日 09时17分28秒