您的位置:首页 > 运维架构

Properties类的使用和源码浅析

2016-04-25 11:09 316 查看
 

        Properties类代表持久化属性集合。Properties能够保存到流中,也可以从流中加载。而每一个key和它对应的value在属性列表中用string来表示。

      一、类的继承关系

            


             从继承关系图中可以知道Properties的内部是有Hashtable实现的。

     二、部分方法的源码

                          load(InputStram)

                 


                load0

         


         setProperty(String,String)

         


          getProperty(String)



 

           三、内部类

          


          四、使用Properties读取Java项目不同位置的properties文件

           


                  

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

public class PropertiesTest
{
public static void main(String[] args) throws IOException
{
//1.读取项目下的properties文件
//File file = new File("test.properties");

//2.读入项目下新建文件夹中的properties文件
//File file = new File("lib/test.properties");

//3.读取src下的properties文件
//File file = new File("src/test.properties");

//4.读取package下面的properties文件,采用类加载
String path = PropertiesTest.class.getClassLoader().getResource("com/test/px/test.properties").getPath();
File file = new File(path);

InputStream inStream = new FileInputStream(file);

Properties props = new Properties();

props.load(inStream);

for(Entry<Object, Object> entry: props.entrySet())
{
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println(key+" = "+value);
}

}
}


 

              
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: