• 【Java 语言】读取 properties 配置文件 ( Java 语言中的 properties 配置文件 | 使用 properties 配置文件 )







    一、Java 语言中的 properties 配置文件



    Java 语言中 , properties 配置文件 是一种用于存储应用程序配置信息的文本文件 ;

    properties 配置文件 通常用于配置应用程序的 各种 参数 ;


    properties 配置文件 是 由一系列 键值对 组成的 , 每个 键值对 都表示一个 配置项 ;

    每个配置项由 一个 键值对 组成 , 键值对 之间使用等号 " = " 分隔 ;


    properties 配置文件 , 文件名一般是 " 名称.properties " ,

    properties 配置文件 内容如下 :

    database.url=jdbc:mysql://localhost:3306/mydb  
    database.username=root  
    database.password=secret
    
    • 1
    • 2
    • 3

    上述配置中 ,

    • database.url 是 键 , 对应的 值 为 jdbc:mysql://localhost:3306/mydb ;
    • database.username 是 键 , 对应的 值 为 root ;
    • database.password 是 键 , 对应的 值 为 secret ;




    二、使用 properties 配置文件



    在 Java 语言中 , 使用 Properties 类 读取 和 操作 properties 配置文件 ;

    通过加载 properties 配置文件 , 应用程序可以在运行时获取所需的配置信息 , 并根据这些信息进行相应的操作 ;


    使用 Properties 类 读取 properties 配置 流程如下 :

    • 首先 , 创建 Properties 类对象 ;
    Properties prop = new Properties(); 
    
    • 1
    • 然后 , 创建 文件输入流 , 读取指定的 properties 配置文件 ;
    FileInputStream input = new FileInputStream("config.properties");  
    
    • 1
    • 再后 , 调用 Properties 实例对象的 load 函数 , 加载 properties 配置文件 的 文件输入流 ;
    prop.load(input);  
    
    • 1
    • 最后 , 调用 Properties 实例对象的 getProperty 函数 , 获取指定 键 对应的 值 ;
    prop.getProperty("database.url")
    
    • 1




    三、完整代码示例




    1、Java 代码


    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) {
            // 首先 , 创建 Properties 类对象
            Properties prop = new Properties();
            try {
                // 然后 , 创建 文件输入流 , 读取指定的 properties 配置文件
                FileInputStream input = new FileInputStream("config.properties");
    
                // 再后 , 调用 Properties 实例对象的 load 函数 , 加载 properties 配置文件 的 文件输入流
                prop.load(input);
    
                // 最后 , 调用 Properties 实例对象的 getProperty 函数 , 获取指定 键 对应的 值
                System.out.println("database.url: " + prop.getProperty("database.url"));
                System.out.println("database.username: " + prop.getProperty("database.username"));
                System.out.println("database.password: " + prop.getProperty("database.password"));
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2、properties 配置文件


    database.url=jdbc:mysql://localhost:3306/mydb  
    database.username=root  
    database.password=secret
    
    • 1
    • 2
    • 3

    3、执行结果


    执行结果 :

    database.url: jdbc:mysql://localhost:3306/mydb
    database.username: root
    database.password: secret

    在这里插入图片描述


    代码下载 : https://download.csdn.net/download/han1202012/88541314

  • 相关阅读:
    Redis分布式锁实现
    java基于springboot+vue+elementui的在线房屋租赁系统 前后端分离
    Vue3 源码解读系列(七)——侦听器
    Chapter4.5:根轨迹法考研参考题
    如何把一个视频分割成不同时长的多个小视频
    计算机组成原理-总线详细讲解(持续更新中)
    LRR1000 智能程控电阻箱
    Codeforces Round #810 (Div. 2)D~A
    Hugging News #0731: 新课程重磅发布、用户交流群邀请你加入、真实图像编辑方法 LEDTIS 来啦!
    ssh secure shell Client连接问题
  • 原文地址:https://blog.csdn.net/han1202012/article/details/134435350