Properties类

概述

Properties类集成HashTable,是一个持久的属性集,以key-value形式存储,key和value都是字符串。

public
class Properties extends Hashtable<Object,Object> 

Java.util.Properties的properties类是用来读取java的配置文件的类。让用户能够脱离程序本身去修改相关的变量设置。java配置文件常常以.properties结尾,格式为文本文件。文件的内容格式是“键=值”,“#”用来注释。
Python支持.ini文件,ConfigParse读取文件,方便程序员或用户通过该类的方法修改配置文件。

Properties常用方法

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

返回值类型 方法和描述

String getProperty(String key) 搜索具有此属性列表中的指定键属性。
String getProperty(String key, String defaultValue) 搜索具有此属性列表中的指定键属性。
void list(PrintStream out) 将此属性列表输出到指定的输出流。
void list(PrintWriter out) 将此属性列表输出到指定的输出流。
void load(InputStream inStream) 从输入的字节流中读取属性列表 (键和元素对)。
void load(Reader reader) 属性列表 (键和元素对) 从流中读取的输入的字符在一个简单的面向行的格式。
void loadFromXML(InputStream in) 加载所有到此属性表所指定的输入流的 XML 文档表示的属性。
Enumeration<?> propertyNames() 在此属性列表中,如果具有相同名称的密钥不已发现从主要属性列表中的默认属性列表中包括非重复键返回所有键的枚举。
void save(OutputStream out, String comments) 弃用。 如果发生 I/O 错误,则保存在属性列表中,此方法不会引发时抛出。保存的属性列表的首选的方法是通过 store(OutputStream out, String comments) 方法或 storeToXML(OutputStream os, String comment) 方法。
Object setProperty(String key, String value) 调用 Hashtable 方法 put。
void store(OutputStream out, String comments) 此属性列表 (键和元素对) 此 Properties 表中写入输出流中适合装载到 Properties 表中使用 load(InputStream) 方法的格式。
void store(Writer writer, String comments) 此属性列表 (键和元素对) 此 Properties 表中写入输出字符流格式适合使用 load(Reader) 方法。
void storeToXML(OutputStream os, String comment) 发出代表所有包含此表中的属性的 XML 文档。
void storeToXML(OutputStream os, String comment, String encoding) 发出代表所有包含在此表中,使用指定的编码的属性的 XML 文档。
Set stringPropertyNames() 返回一组键此属性列表中的关键和其对应的值都是字符串,默认属性列表中包括非重复键,如果具有相同名称的密钥不已发现从主要属性列表。

Properties写入、读取、遍历

在这里插入图片描述

写入

写入属性到.properties文件

public static void main(String[] args) throws IOException {
        // 1.创建Properties对象
        Properties properties = new Properties();
        // 2.设置Properties属性
        properties.setProperty("username","fxr");
        properties.setProperty("password","3333");
        // 3.创建输出流
        File file = new File("src/main/resources/config.properties");
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        // 4.存储Properties持久化到文件
        properties.store(outputStream, "username and password");
        // 5.关闭输出流
        outputStream.close();
    }

读取

package main.java.com;

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

public class PropertiesUtil {

    /**
     * 获得输入流的4种方法
     *  1.创建InputStream的实现类BufferedInputStream
     *  2.Class类的getResourceAsStream()
     *  3.ClassLoader类加载器的静态方法getSystemResourceAsStream()
     *  4.ClassLoader的实例方法getResourceAsStream()
     */
    public static InputStream getInputStream1(String url) throws FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(new File(url)));
    }

    public static InputStream getInputStream2(String url){
        return PropertiesUtil.class.getResourceAsStream(url);
    }

    public static InputStream getInputStream3(String url){
        return PropertiesUtil.class.getClassLoader().getResourceAsStream(url);
    }

    public static InputStream getInputStream4(String url){
        return ClassLoader.getSystemResourceAsStream(url);
    }

    /**
     * 获得资源束的2种方法
     *  1.ResourceBundle的静态方法getBundle()
     *  2.ResourceBundle的实现类PropertyResourceBundle
     */
    public static ResourceBundle getResourceBundle1(String url){
        return ResourceBundle.getBundle(url);
    }

    public static ResourceBundle getResourceBundle2(String url) throws IOException {
        return new PropertyResourceBundle(new FileInputStream(new File(url)));
    }


    /**
     * 通过输入流打印属性
     * @param inputStream
     */
    public static void printPropertiesByInputStream(InputStream inputStream) throws IOException {
        // 1.创建Properties对象
        Properties properties = new Properties();
        // 2.load加载输入流
        properties.load(inputStream);
        // 3.关闭输入流
        inputStream.close();
        // 4.entrySet()方法获得属性键值
        Set<Map.Entry<Object,Object>> set = properties.entrySet();
        // 5.打印属性键值
        set.forEach(System.out::println);
    }

    /**
     *通过资源束ResourceBundle获得属性
     * @param bundle
     */
    public static void printPropertiesByResourceBundle(ResourceBundle bundle){
        // 1.通过资源束ResourceBundle keySet()方法获得属性名
        Set<String> keySet = bundle.keySet();
        // 2.通过资源束ResourceBundle getString()方法获得属性值
        for (String key : keySet){
            System.out.println(key+ " "+ bundle.getString(key));
        }
    }
}

调用

public static void main(String[] args) throws IOException {
       InputStream stream1 =
               PropertiesUtil.getInputStream1("src/main/resources/config.properties");
       InputStream stream2 =
               PropertiesUtil.getInputStream2("../../resources/config.properties");
       InputStream stream3 =
               PropertiesUtil.getInputStream3("main/resources/config.properties");
       InputStream stream4 =
               PropertiesUtil.getInputStream4("main/resources/config.properties");
       ResourceBundle resourceBundle1 = PropertiesUtil.getResourceBundle1("main/resources/config");
       ResourceBundle resourceBundle2 = PropertiesUtil.getResourceBundle2("src/main/resources/config.properties");
       PropertiesUtil.printPropertiesByInputStream(stream1);
       PropertiesUtil.printPropertiesByInputStream(stream2);
       PropertiesUtil.printPropertiesByInputStream(stream3);
       PropertiesUtil.printPropertiesByInputStream(stream4);
       PropertiesUtil.printPropertiesByResourceBundle(resourceBundle1);
       PropertiesUtil.printPropertiesByResourceBundle(resourceBundle2);
    }

读取.properties文件总结

Properties的实例方法load(inputStream),再用entrySet()方法获得属性列表
	创建InputStream的实现类BufferedInputStream( )
	Class类的getResourceAsStream()
	Class类的getClassLoader()再调用getResourceAsStream()
	ClassLoader的静态方法getSystemResourceAsStream()
ResourceBundle的实例方法keySet()获得属性名列表,再用ResourceBundle的实例方法getString获得属性值列表
	创建ResourceBundle的实现对象PropertyResourceBundle
	ResourceBundle的实例方法keySet(),在调用ResourceBundle的实例方法getString()

遍历.properties文件

 /**
     * 遍历.properties文件
     * 1.Properties的实例方法StringPropertyNames()获得属性值列表,再调用getProperty(String key)获得属性值
     * 2.Properties的实例方法propertyNames获得Enumeration<?> 再通过Enumeration<?>的实例方法hasMoreElements()、nextElement()、getProperty()
     * 3.Properties的实例方法keySet()获得属性名列表,再调用get(key)获得属性值
     * 4.Properties的实例方法entrySet()获得属性键值列表,再调用getKey()和getValue()获得属性的键值
     */
    public static void traversal(Properties properties){
        // 1
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys){
            System.out.println(key+""+properties.getProperty(key));
        }

        // 2
        Enumeration<?> enumeration = properties.propertyNames();
        while(enumeration.hasMoreElements()){
            String key = (String) enumeration.nextElement();
            String value = properties.getProperty(key);
            System.out.println(key+""+value);
        }

        //3
        Set<Map.Entry<Object,Object>> set = properties.entrySet();
        for (Map.Entry<Object,Object> entry : set){
            System.out.println(entry.getKey()+""+entry.getValue());
        }

        Set<Object> keySet = properties.keySet();
        for (Object key : keySet){
            System.out.println(key+""+properties.getProperty((String)key));
        }

    }
 Properties properties = new Properties();
       File file = new File("src/main/resources/config.properties");
       InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
       properties.load(inputStream);
       PropertiesUtil.traversal(properties);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值