zoukankan      html  css  js  c++  java
  • Java 读取 .properties 配置文件

        java 开发中,经常要读取 properties 配置文件,下面介绍几种读取方式:

        1、基于 InputStream 读取配置文件

        该方式的优点在于可以读取任意路径下的配置文件

            
         Properties properties = new Properties(); // 使用InPutStream流读取properties文件 BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\work\ott-monitor\src\main\resources\jdbc.properties")); properties.load(bufferedReader); // 获取key对应的value值 String driver = properties.getProperty("datasource.mysql.driver"); System.out.println(driver);

        jdbc.properties内容

        

      2、当配置文件放在src/main/resources的目录下时,只能使用Class.getResourceAsStream()方法来加载

        当工程部署到Tomcat中时,也应该用这种方式

         import java.io.InputStream;
         Properties prop = new Properties(); //使用ClassLoader加载properties配置文件生成对应的输入流 InputStream is = (InputStream) dataMigrate.class.getClass().getResourceAsStream("/jdbc.properties"); // 使用properties对象加载输入流 prop.load(is); //获取key对应的value值 String driver = prop.getProperty("datasource.mysql.driver");
  • 相关阅读:
    webpack 代码拆分,按需加载
    Linux 安装 node
    H5项目常见问题及注意事项
    低耦合,高内聚。实乃至理名言
    Generator 函数学习笔记
    async 函数学习笔记
    JavaScript 中的 Thunk 函数
    Promise 学习笔记
    vb.net WIN32API 获取listview的值
    vb WIN32 API获取syslistview行数
  • 原文地址:https://www.cnblogs.com/shaosks/p/9982094.html
Copyright © 2011-2022 走看看