Java基础IO篇--Properties类

115 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第2天,点击查看活动详情

前言

在日常的开发中,为了增加系统的灵活性和拓展性,我们会把一些经常变动的参数配置抽离出来,放在配置文件中,在项目启动时,灵活的选择需要加载的参数。常见的配置文件形式有Properties类、XML文件和YAML文件。下面我们将介绍一下Properties类,xml和yaml文件后续会陆续介绍的。

一、Properties类的概述

java.util.Properties,底层是一个Map结构,继承于HashTable ,主要用来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。 更多时候用于读取配置文件

二、Properties类构造方法

Properties(): 没有默认值的空属性列表。  

三、Properties类基本存取功能方法

  • public Object setProperty(String key, String value):存一对属性,等价于map.put(key,value);
  • public String getProperty(String key): 使用此属性列表中指定的键搜索属性值,等价于map.get(key);
  • public Set<String> stringPropertyNames():所有键的名称的集合,等价于map.keySet();
public class Test {
    public static void main(String[] args) {

        // 创建Properties类的对象
        Properties pp = new Properties();

        // 存数据
        pp.setProperty("driverClass", "com.mysql.jdbc.Driver");
        pp.setProperty("url", "http://localhost:3306/day11_db");
        pp.setProperty("username", "root");
        pp.setProperty("password", "root");

        // 取数据(所有的key)
        Set<String> set = pp.stringPropertyNames();

        // 非空判断
        if (!set.isEmpty()) {
            // 遍历
            for (String key : set) {
                System.out.println("key:" + key + ", value:" + pp.getProperty(key));
            }
        }
    }
}

四、Properties类读取配置文件

Properties类除了能够当做Map使用,还可以与文件(可以是txt文件、可以是properties)进行关联,获得文件里面的数据!

  • void load(InputStream inStream):从输入字节流读取属性列表(键和元素对)。
***要求:将文件放在src目录(类的根路径)下面!***

#资源文件fis.properties内容:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11_db
password=root
username=root
/*
读取配置文件(text类型的)
*/
public class Test {
    public static void main(String[] args) {
        // 创建Properties类的对象
        Properties prop = new Properties();
        // 创建资源文件对应的字节输入流
        try (
                //FileInputStream fis = new FileInputStream("day11_io/src/fis.txt");
                FileInputStream fis = new FileInputStream("day11_io/src/fis.properties");
        ) {
            // 让文件对应的的输入流与prop关联
            prop.load(fis);
            // 获得所有的key
            Set<String> set = prop.stringPropertyNames();
            // 判断
            if (!set.isEmpty()) {
                // 遍历
                for (String key : set) {
                    // 获得值
                    String value = prop.getProperty(key);
                    // 打印输出
                    System.out.println(key + "对应的值是:" + value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

扩展:使用ResourceBundle类读取properties配置文件

  • static ResourceBundle getBundle(String baseName) : 通过读取在src下面的properties文件的名称(不要扩展名),得到一个ResourceBundle对象!
  • String getString(String key): 获得指定key对应的value值 【driverClass=com.mysql.jdbc.Driver,只要将driverClass作为参数,就可以得到值!】
/*
     ResourceBundle类:
    读取properties配置文件!
    */
public class Test {
    public static void main(String[] args) {

        // 通过ResourceBundle类的静态方法,得到ResourceBundle类的对象
        ResourceBundle bundle = ResourceBundle.getBundle("fis"); // 只要给定properties配置文件的名称,不要扩展名!

        // 获得指定参数的值
        String driverClass = bundle.getString("driverClass");
        String url = bundle.getString("url");
        String username = bundle.getString("username");
        String password = bundle.getString("password");

        System.out.println(driverClass);
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
    }

}

后记

在我们的日常开发中,底层的框架就已经将Properties配置文件的信息在系统初始化时就加载进系统了,不需要要我们操作,了解即可。
喜欢我的文章的朋友点点喜欢、收藏,也欢迎朋友们评论区留下你的意见和建议,恕毅在此拜谢!