加载 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
posted on 2018-08-05 14:13 wait_for_you 阅读(165) 评论(0) 编辑 收藏 举报