• 加载 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  

  • 相关阅读:
    eclipse改变默认的编码格式(UTF-8)
    Guava学习:Joiner和Splitter工具(二)
    Guava中的Joiner和Splitter工具演示
    GitHub查找开源项目技巧分享
    java1.8特性之多重排序简单示例
    jedis工具类:java操作redis数据库
    SQL优化建议(mysql)
    Moodle插件之Filters(过滤器)
    Moodle插件开发系列——XMLDB编辑器
    Moodle插件开发——Blocks(版块)
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9425404.html
Copyright © 2020-2023  润新知