六种获取配置properties文件的方法
来源:互联网 发布:淘宝一件代发挣钱吗 编辑:程序博客网 时间:2024/06/05 11:52
总结一下六种获取配置properties文件的方法,代码如下:
1 package com.xujingyang.test ; 2 3 import java.io.BufferedInputStream ; 4 import java.io.FileInputStream ; 5 import java.io.InputStream ; 6 import java.util.Locale ; 7 import java.util.Properties ; 8 import java.util.PropertyResourceBundle ; 9 import java.util.ResourceBundle ; 10 import org.junit.Test ; 11 12 /** 13 * @descript 六种获取配置文件的方法,注意各种的路径的问题 14 * @author xujingyang 15 * @time 2017年5月15日下午4:23:18 16 */ 17 public class TestReadProperties { 18 19 /** 20 * 1、使用java.util.Properties类的load()方法 21 */ 22 @Test 23 public void read1(){ 24 try { 25 InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties")); 26 Properties p=new Properties(); 27 p.load(stream); 28 String name = p.get("name").toString() ; 29 String age = p.get("age").toString() ; 30 System.out.println(name+":"+age) ; 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 } 35 36 /** 37 * 2、使用java.util.ResourceBundle类的getBundle()方法 38 */ 39 @Test 40 public void read2(){ 41 try { 42 ResourceBundle bundle = ResourceBundle.getBundle("my", Locale.getDefault()) ; 43 String name = bundle.getString("name"); 44 String age = bundle.getString("age"); 45 System.out.println(name+":"+age) ; 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 51 52 53 /** 54 *3、 使用java.util.PropertyResourceBundle类的构造函数 55 */ 56 @Test 57 public void read3(){ 58 try { 59 InputStream stream=new BufferedInputStream(new FileInputStream("src/my.properties")); 60 ResourceBundle bundle=new PropertyResourceBundle(stream); 61 String name = bundle.getString("name"); 62 String age = bundle.getString("age"); 63 System.out.println(name+":"+age) ; 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 69 70 71 /** 72 *4、 使用class变量的getResourceAsStream()方法 73 */ 74 @Test 75 public void read4(){ 76 try { 77 InputStream stream = TestReadProperties.class.getResourceAsStream("/my.properties") ; 78 Properties p=new Properties(); 79 p.load(stream); 80 String name = p.get("name").toString() ; 81 String age = p.get("age").toString() ; 82 System.out.println(name+":"+age) ; 83 } catch (Exception e) { 84 e.printStackTrace(); 85 } 86 } 87 88 89 90 /** 91 *5、 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 92 */ 93 @Test 94 public void read5(){ 95 try { 96 InputStream stream = TestReadProperties.class.getClassLoader().getResourceAsStream("my.properties") ; 97 Properties p=new Properties(); 98 p.load(stream); 99 String name = p.get("name").toString() ;100 String age = p.get("age").toString() ;101 System.out.println(name+":"+age) ;102 } catch (Exception e) {103 e.printStackTrace();104 }105 }106 107 108 /**109 *6、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法110 */111 @Test112 public void read6(){113 try {114 InputStream stream = ClassLoader.getSystemResourceAsStream("my.properties");115 Properties p=new Properties();116 p.load(stream);117 String name = p.get("name").toString() ;118 String age = p.get("age").toString() ;119 System.out.println(name+":"+age) ;120 } catch (Exception e) {121 e.printStackTrace();122 }123 }124 125 }
配置文件:
name的值是小明,这里配置文件会自动编码
补充:
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
阅读全文
0 0
- 六种获取配置properties文件的方法
- 六种读取Properties文件的方法
- 读取Properties文件的六种方法
- 读取Properties文件的六种方法
- Java项目中读取properties文件,以及六种获取路径的方法
- Java项目中读取properties文件,以及六种获取路径的方法
- AllenZhao Java项目中读取properties文件,以及六种获取路径的方法
- 从.properties文件中获取配置数据的方法小结
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SEAPI读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 框架之 hibernate之各种查询
- 全源码教你一键锁屏
- 框架之Struts2简单入门
- java导出Excel 总结
- hibernate 关联关系 详解
- 六种获取配置properties文件的方法
- Linux 安装mysql后修改密码策略
- mybatis+spring 方式一
- GSON 报错HibernateProxy. Forgot to register a type adapter? 的解决办法
- Oracle12c DataGuard 物理备库的简单配置
- 【caffe】windows下vs2013+opencv3.2.0+opencv_contrib(包含dnn)+cmake3.8编译与配置
- structs2----数据封装以及拦截器
- 逃离迷宫
- structs2---OGNL表达式