• 【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

  • 相关阅读:
    Java——》线程间是如何通信的
    图论17(Leetcode864.获取所有钥匙的最短路径)
    【软件设计师 - 一周通关】1.考试介绍
    HTML - input type=file 允许用户选择多个文件
    继续刷刷--《十大经典排序算法》
    spring 框架理论
    华为云云耀云服务器L实例评测|使用clickhouse-benchmark工具对ClickHouse的性能测试
    计算机毕业设计Java奥利给共享自习室系统(源码+系统+mysql数据库+lw文档)
    Xilinx FPGA管脚约束语法规则(UCF和XDC文件)
    使用EasyExcel实现Excel导入导出
  • 原文地址:https://blog.csdn.net/han1202012/article/details/134435350