Java_Properties集合

------------------------------------------- android培训、java培训、期待与您交流! ----------------------


Properties集合:是hashtable的子类,具备Map集合的特点,里面存储的键值对都是字符串;
文件流和键值对相结合就是用此集合和IO技术相结合的结合容器
特点:可以用于键值对配置文件,可以操作键盘灯额数据


Properties集合的使用:
设置和获取元素:Properties prop =new Properties();
prop.setProperty("张三","30");//添加,修改
System.out.println(prop);
prop.getProperty("键");
循环获取:Set<String> names=prop.stringPropertyNames();
     for(String s : names){//增强for循环
prop.getProperty(s);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------
设计一个程序,用于记录程序运行的次数,如果使用次数已到,就给出注册提示
示例:import java.io.*;
import java.util.*;


class RunCount{
public static void main(String[] args) throws IOException {
Properties prop = new Properties(); //创建一个Properties


File file = new File("count.ini");//创建一个文件,存储程序的使用次数
if(!file.exists()) //判断文件是否存在
file.createNewFile();


FileInputStream fis = new FileInputStream(file);//创建一个字符输入流,并和文件绑定


prop.load(fis); //将属性信息和文件相关联


int count = 0;//用于记录文件使用词素
String key = prop.getProperty("UseTime");//设置属性的键为UseTime


if(key != null){
count = Integer.parseInt(key); //将键的值解析成Integer类型,便于计算
if(count >= 5) {
sop("您好!使用次数已到!请付费后使用!");
}
}


count++;


prop.setProperty("UseTime",count+" ");


FileOutputStream fos = new FileOutputStream(file);//创建字符输出流,和文件绑定


prop.store(fos," ");//将属性的信息输入文件(文件名,“存储的内容”)


fos.close();//关闭流
fis.close();

}


public static void sop(Object obj){
System.out.println(obj);
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值