Java Properties 类

(一)概述

java.util.Properties继承自java.util.Hashtable,是一个持久化的属性保存对象,可以将属性内容写出到stream中或者从stream中读取属性内容。 它的重要特性如下:

  • 在底层的Hashtable中,每一对属性的keyvalue都是按照string类型来保存的。
  • Properties支持文本方式xml方式的数据存储。在文本方式中,格式为key:value,其中分隔符可以是:冒号(:)、等号(=)、空格。其中空格可以作为key的结束,同时获取的值回将分割符号两端的空格去掉。
  • Properties可以将其他的Properties对象作为默认的值。
  • Hashtable的所有方法Properties对象均可以访问,但是不建议这么做,因为Hashtable可以存放其他数据类型,这样会导致Properties一些方法调用报错。
  • 在properties文件中,可以用井号"#"来作注释。
  • 线程安全
  • key、value不可以是null

(二)介绍

Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。

我们在很多需要避免硬编码的应用场景下需要使用properties文件来加载程序需要的配置信息,比如JDBC、MyBatis框架等。Properties类则是properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由Properties类。

由于 Properties 继承自 Hashtable 类,因此具有 Hashtable 的所有功能,同时还提供了一些特殊的方法来读取和写入属性文件。

Properties 类常用于存储程序的配置信息,例如数据库连接信息、日志输出配置、应用程序设置等。使用Properties类,可以将这些信息存储在一个文本文件中,并在程序中读取这些信息。

Properties 类被许多 Java 类使用。例如,在获取环境变量时它就作为 System.getProperties() 方法的返回值。

(三)方法

Properties 定义如下实例变量。这个变量持有一个 Properties 对象相关的默认属性列表。
Properties defaults;

Properties 类定义了两个构造方法。 第一个构造方法没有默认值。

Properties()

第二个构造方法使用propDefault 作为默认值。两种情况下,属性列表都为空:

Properties(Properties propDefault)

除了从 Hashtable 中所定义的方法,Properties 还定义了以下方法:

序号方法描述
1 String getProperty(String key)
 用指定的键在此属性列表中搜索属性。
2 String getProperty(String key, String defaultProperty)
用指定的键在属性列表中搜索属性。
3 void list(PrintStream streamOut)
 将属性列表输出到指定的输出流。
4 void list(PrintWriter streamOut)
将属性列表输出到指定的输出流。
5 void load(InputStream streamIn) throws IOException
 从输入流中读取属性列表(键和元素对)。
6 Enumeration propertyNames( )
按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
7 Object setProperty(String key, String value)
 调用 Hashtable 的方法 put。
8 void store(OutputStream streamOut, String description)
 以适合使用  load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

(四)示例

Properties 类提供了多种读取和写入属性文件的方法。其中最常用的方法是 load() 和 store() 方法。

load() 方法可以从文件中读取属性,而 store() 方法可以将属性写入文件。

下面是一个简单的例子,演示了如何使用 Properties 类来读取和写入属性文件:

复制代码
import java.io.*;
import java.util.Properties;

public class PropertiesExample {
    public static void main(String[] args) {
        Properties prop = new Properties();

        try {
            // 读取属性文件
            prop.load(new FileInputStream("config.properties"));

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

            // 输出属性值
            System.out.println("username: " + username);
            System.out.println("password: " + password);

            // 修改属性值
            prop.setProperty("username", "newUsername");
            prop.setProperty("password", "newPassword");

            // 保存属性到文件
            prop.store(new FileOutputStream("config.properties"), null);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
复制代码

以上实例中我们创建了一个 Properties 对象,然后使用 load() 方法从配置文件中读取属性。接着,我们使用 getProperty() 方法获取属性值,并输出到控制台。然后,我们使用 setProperty() 方法修改属性值,并使用 store() 方法将修改后的属性保存回文件。

下面的程序说明这个数据结构支持的几个方法:
复制代码
import java.util.*;
 
public class PropDemo {
 
   public static void main(String args[]) {
      Properties capitals = new Properties();
      Set states;
      String str;
      
      capitals.put("Illinois", "Springfield");
      capitals.put("Missouri", "Jefferson City");
      capitals.put("Washington", "Olympia");
      capitals.put("California", "Sacramento");
      capitals.put("Indiana", "Indianapolis");
 
      // Show all states and capitals in hashtable.
      states = capitals.keySet(); // get set-view of keys
      Iterator itr = states.iterator();
      while(itr.hasNext()) {
         str = (String) itr.next();
         System.out.println("The capital of " +
            str + " is " + capitals.getProperty(str) + ".");
      }
      System.out.println();
 
      // look for state not in list -- specify default
      str = capitals.getProperty("Florida", "Not Found");
      System.out.println("The capital of Florida is "
          + str + ".");
   }
}
复制代码

以上实例编译运行结果如下:

The capital of Missouri is Jefferson City.
The capital of Illinois is Springfield.
The capital of Indiana is Indianapolis.
The capital of California is Sacramento.
The capital of Washington is Olympia.

The capital of Florida is Not Found.

 
posted @   ImreW  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示