Javaデータ構造:プロパティクラス

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class PropertiesJava {
    
    
    public static void main(String[] args) {
    
    
        // Properties 类
        // Properties 继承于 Hashtable。
        // 表示一个持久的属性集.属性列表中**每个键及其对应值都是一个字符串。**
        // Properties 类被许多 Java 类使用。
        // 例如,在获取环境变量时它就作为 System.getProperties() 方法的返回值。
        // Properties 定义如下实例变量.这个变量持有一个 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 properties = new Properties();
        properties.put("键1", "值1");
        properties.put("username", "name");
        properties.put("password", "password");

        Set states;
        String str;
        // 获取键,存在集合里,用一个集合来接收
        states = properties.keySet();
        Iterator itr = states.iterator();
        while (itr.hasNext()) {
    
    
            str = (String) itr.next();
            System.out.println("properties 键(" + str + ") 对应的值:(" + properties.getProperty(str) + ").");
        }
        System.out.println();

    }
}

おすすめ

転載: blog.csdn.net/wjl__ai__/article/details/112385124