首页 > 其他 > 详细

Properties读取小结

时间:2017-04-04 17:15:00      阅读:159      评论:0      收藏:0      [点我收藏+]

文件结构目录如图所示:

技术分享

  其中,config2为与src同级的sourec folder,c.properties位于src根目录,b.properties位于src/config1 folder下

a.properties位于cn.package1包下。所有结果均已成功测试,测试环境为Myeclipse2016+JDK8

 

一、读取a.properties:

 1 package cn.package1;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Properties;
 6 
 7 import org.junit.Test;
 8 
 9 public class Demo01 {
10     @Test
11     public void fun1() throws IOException{
12         InputStream in1 = Demo01.class.getClassLoader()
13                 .getResourceAsStream("cn/package1/a.properties");
14         Properties props = new Properties();
15         props.load(in1);
16         String value1 = props.getProperty("name");
17         System.out.println(value1);
18     }
19 }

(输入流的处理以及关闭可以改进)

 

二、读取b.properties:

  (重复代码已经省略!)

1 InputStream in1 = Demo01.class.getClassLoader()
2                 .getResourceAsStream("config1/b.properties");

 

三、读取c.properties:

1 InputStream in1 = Demo01.class.getClassLoader()
2                 .getResourceAsStream("c.properties");

 

四、读取d.properties:

InputStream in1 = Demo01.class.getClassLoader()
                .getResourceAsStream("d.properties");

 

Properties读取小结

原文:http://www.cnblogs.com/jiangbei/p/6665322.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!