java properties api_Java API 之 Properties 类

本文介绍了Java中Properties类的使用,包括如何读取和写入.properties配置文件,详细解析了Properties类的构造函数、load方法、getProperty方法、setProperty方法以及存储属性到文件的store方法。此外,还提到了遍历属性列表的方式。
摘要由CSDN通过智能技术生成

1、简介

在项目中我们经常看到一种格式极其干净的配置文件,如:config.properties。在Java的体系结构中也提供了API对properties文件进行读取和写入等操作,即:Properties类。

2、入门DEMO

在cn.lay.properties包下建立类Properties.java和config.properties文件,如下:

Properties.java

packagecn.lay.properties;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;public classPropertiesDemo {public static void main(String[] args) throwsIOException {

InputStream inputStream= Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");

Properties config= newProperties();

config.load(inputStream);

String userName= config.getProperty("username");

System.out.println("username=" +userName);

}

}

config.properties

username=lay

运行main方法,输出:

username=lay

main方法中,通过输入流读取了config.properties。Properties实例对象从流中读取文件属性,并提供getProperty(key)方法读取属性。

3、类Properties

类Properties存在于java.util包下

继承结构如:

java.lang.Object

|_ java.uil.Dictionary

|_ java.util.Hashtable

|_ java.util.Properties

已实现的主要接口:

Serializable, Cloneable, Map

直接子类:

Provider

Properties直接继承自Hashtable那么它的数据结构也和Hashtable一样属于键值对形式如:username="lay",不过不同的是,Properties的键和值都是String类型。所以,虽然Properties继承了Hashtable后可以使用put和putAll方法,但是不被建议使用。因为这两个方法允许插入非String类型。

字段摘要:

protected Properties defaults; 默认属性列表

构造函数:

1) Properties();

2) Properties(Properties defaults); 可以初始化默认属性列表

4、加载properties资源文件

Properties类重载了两个方法用于读取属性列表,也就是加载资源为实例对象:

1) void load(InputStream inputStream);

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");

Properties config= newProperties();

config.load(inputStream);

2) void load(Reader reader);

InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties");

Reader reader= new InputStreamReader(inputStream, "utf-8");

Properties config= newProperties();

config.load(reader);

5、读取属性值

1)String getProperty(String key);

String userName = config.getProperty("username");

2) String getProperty(String key, String defaultValue);

String userName = config.getProperty("username", "nobody");

6、设置属性值

config.setProperty("username", "marry");

7、存储为properties资源文件

写数据的方法分为两种,list和store

1)list 此方法通常用于调试,System.out即可以获取PrintStream,从而输出到控制台

void list(PrintStream out);

PrintStream printStream = new PrintStream("/Users/lay-mac/Desktop/config.properties");

config.list(printStream);

void list(PrintWriter writer);

PrintWriter printWriter = newPrintWriter(outputStream);

config.list(printWriter);

2) store

void store(OutputStream out, String comments);

OutputStream outputStream = new FileOutputStream("/Users/lay-mac/Desktop/config.properties");

config.store(outputStream,"test store");

void store(Writer writer, String comments);

Writer writer = new FileWriter("/Users/lay-mac/Desktop/config.properties");

config.store(writer,"test store");

8、遍历属性列表

Set stringPropertyNames(); 返回属性列表键的set集合,包括默认列表;

Set keySet =config.stringPropertyNames();for(String key : keySet) {

System.out.println("key=" +key);

System.out.println("value=" +config.getProperty(key));

}

Enumeration> propertyNames();返回属性列表中所有键的枚举,包括默认列表;

Enumeration enumeration = (Enumeration) config.propertyNames();while(enumeration.hasMoreElements()) {

String key=(String) enumeration.nextElement();

System.out.println("key=" +key);

System.out.println("value=" +config.getProperty(key));

}

除了读取写入.properties文件外,Properties类还可以读取和写入xml文件形式,具体请参考:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值