读取jar包内的配置文件,可以使用ResourceBundle,具体具体例子如下
1 import java.io.BufferedInputStream; 2 3 import java.io.IOException; 4 5 import java.io.InputStream; 6 7 import java.util.PropertyResourceBundle; 8 9 import java.util.ResourceBundle; 10 11 12 13 14 15 public class ReadProperty { 16 17 private ResourceBundle rb = null; 18 19 private ResourceBundle loadProperties(){ 20 21 if(null == rb){ 22 23 try { 24 25 InputStream in = new BufferedInputStream(this.getClass().getResourceAsStream("db.properties")); 26 27 rb = new PropertyResourceBundle(in); 28 29 } catch (IOException e) { 30 31 System.out.println("Can't read properties file"); 32 33 } 34 35 } 36 37 return rb; 38 39 40 41 } 42 43 /** 44 45 * 获取配置参数值 46 47 * @param key 48 49 * @return 50 51 */ 52 53 public String getProperties(String key){ 54 55 ResourceBundle rb = loadProperties(); 56 57 return rb.getString(key); 58 59 } 60 61 62 63 }
欢迎加入Hadoop技术群进行交流:147681830