zoukankan      html  css  js  c++  java
  • 加载 properties 的几种方法

    1.通过Resources 拿(放在resources文件夹)

    1 Properties p = new Properties();
    2 
    3 p = Resources.getResourceAsProperties("jdbc.properties");

    2.

    通过流 来拿   p.load(in);

    InputStream in = Thread.currentThread()
                                .getContextClassLoader()
                                .getResourceAsStream("jdbc.properties");
                p.load(in);

     2.2  FIleInuptStresm

    //InputStream in =  new  FileInputStream("jdbc.properties");//不行
    		InputStream in =  new  FileInputStream("resources/jdbc.properties");//行
    		p.load(in);
    

     2.3

    源码

     1 package com.seehope._03_util;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.InputStream;
     5 import java.util.Properties;
     6 
     7 public class PropertiesDemo {
     8     public static void main(String[] args) throws Exception{
     9         Properties p = new Properties();
    10         
    11         /*1.通过流来拿,那应该会指定一个路径
    12         InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
    13         p.load(in);
    14         */
    15         
    16         /* 2.
    17         //InputStream in =  new  FileInputStream("jdbc.properties");//不行
    18         InputStream in =  new  FileInputStream("resources/jdbc.properties");// 行
    19         p.load(in);
    20         */
    21         InputStream in = PropertiesDemo.class
    22                 .getClassLoader().
    23                 getResourceAsStream("test/jdbc.properties");
    24         //scr 文件夹下面的 test 包下的 jdbc.properties
    25         p.load(in);
    26         
    27         
    28         String  className = p.getProperty("DriverClassName");
    29         System.out.println(className);
    30         String url = p.getProperty("url");
    31         System.out.println(url);
    32     }
    33     
    34 }

      

    输出

    1 com.mysql.jdbc.Driver
    2 jdbc:mysql://localhost:3306/jdbc

    更多参见   https://blog.csdn.net/u013301376/article/details/78341914  

  • 相关阅读:
    推荐一款稳定快速免费的前端开源项目 CDN 加速服务
    MySQL限时解答
    OneProxy的功能与限制
    MySQL浮点计算存在的问题与解决方案
    DAS、SAN、NAS的区别
    气质
    受制于人
    mysqlbinlog flashback 5.6完全使用手册与原理
    Innodb引擎 compact模式下元组的磁盘存储结构
    数据迁移程序
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9425404.html
Copyright © 2011-2022 走看看