JAVASE总结--Proterties

来源:互联网 发布:淘宝茶叶排名 编辑:程序博客网 时间:2024/05/20 23:37

Propertiesjava.util.Properties,主要用于读取java配置文件,其配置文件常为.properties,格式为文本文件,文件的内容是“键-值”的格式,文本注释信息可以用“#”来注释。

主要方法:

l getProperty(String key),用于指定的键在此属性列表中搜索属性。即,通过参数key,得到key所对应的value

l loadInputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件进行装载来获取该文件中所有键-值对。以供getProperty(String key)来搜索。

l setProperty(String key,String value),调用Hashtable的方法put。来设置键-值对。

l storeOutputStream out,String comments,以适合使用load方法加载到Properties表中的格式,将次Properties表中属性列表(键和元素对)写入输出流。与load相反,该方法将键-值对写入到指定的文件中去。

l clear(),清除所有装载的键-值对。

获取JVM的系统属性

Properties properties = System.getProperties();

properties.list(System.out);

获取配置文件内容

Config.properties

————————————

#database config file

dbname:bjsxt

dbpassword:123456

————————————

//获取文件流

InputStream inputStream = ConfigFile.class.getClassLoader().getResourceAsStream("config.properties");

//转换成为properties

Properties properties =new Properties();

properties.load(inputStream);

//获取值

System.out.println(properties.getProperty("dbname"));

System.out.println(properties.getProperty("dbpassword"));

 

 

 

0 0
原创粉丝点击