zoukankan      html  css  js  c++  java
  • 读取.properties配置文件的几种方式:

    这里我将配置文件放在src目录下了,下面放的都是实验过的代码:

    第一种(通过输入流):

            //1、获取到流文件
            FileInputStream is = new FileInputStream("src/jdbc.properties");
    //2、加载流文件
            Properties properties = new Properties();
            properties.load(is);
            //3、获取到配置文件信息
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String name = properties.getProperty("name");
            String passwd = properties.getProperty("passwd");

    第二种(通过类加载器):

            //1、获取到流文件
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
         //2、加载流文件
            Properties properties = new Properties();
            properties.load(is);
            //3、获取到配置文件信息
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String name = properties.getProperty("name");
            String passwd = properties.getProperty("passwd");

    第三种(通过资源绑定器):

            //1、获取到流文件
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            //2、获取到配置文件信息
            String driver = bundle.getString("driver");
            String url = bundle.getString("url");
            String name = bundle.getString("name");
            String passwd = bundle.getString("passwd");
  • 相关阅读:
    Linux下的MySQL主从同步
    人不能同时在两个地方做猪(Scrum Team)
    memcache安装
    Java开发中的Memcache原理及实现
    linux mv
    nginx
    idea 热部署
    vue watch
    vue入门
    基于vue-cli快速构建
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14193364.html
Copyright © 2011-2022 走看看