4.2.5 集合Properties类

h3 { margin-top: 0.46cm; margin-bottom: 0.46cm; line-height: 173%; page-break-inside: avoid; }h3.western { font-family: "DejaVu Serif",serif; font-size: 16pt; }h3.cjk { font-family: "DejaVu Sans"; font-size: 16pt; }h3.ctl { font-family: "DejaVu Sans"; font-size: 16pt; }p { margin-bottom: 0.21cm; }a:link { color: rgb(0, 0, 255); }


Properties 类是Hastable 类的子类

增加了将Hastable 对象中的 key 和值保存到文件从文件中读取关键字和值到 Hashtable 对象中的方法

store ( OutputStream  out, String  comments) Properties 对象中的内容,每个属性的关键字都必须是String 类型 有点类似于存储注释

load ( InputStream  inStream)
          
从输入流中读取属性列表(键和元素对)。

s etProperty ( String  key, String  value)
          
调用 Hashtable 的方法 put

 

下面是完成一个检查程序执行了多少次的代码

public static void main(String[] args) {

Properties settings = new Properties();

try {

// 加载配置文件

settings.load( new FileInputStream( "context.conf" ));

} catch (FileNotFoundException e) {

// 如果加载配置文件失败 就是第一次读取 所有要向文件里面写东西

settings.setProperty( "count" , String. valueOf (0));

} catch (IOException e) {

e.printStackTrace();

}

int c = Integer. parseInt (settings.getProperty( "count" ))+1;

System. out .println( " 已经使用了 " +c+ " " );

settings.setProperty( "count" , new Integer(c).toString());

try {

// 存储注释信息

settings.store( new FileOutputStream ( "context.conf" ), "Program is used : " );

} catch ( FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值