Properties的基本使用

Properties的基本使用

     

            Properties:继承了HashTable,是一个Map集合,存储的属性集都是字符串类型的,所以不需要泛型 ,它是集合中可以和流结合使用的一个集合类。

它里面有几个重要的方法如下:

1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 -   值  对。以供 getProperty ( String key) 来搜索。

3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

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

5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。

6.  list()将属性列表输出到指定的输出流。

 

先来看一个获取系统属性的例子

 

 //获取所有的系统属性集
	Properties pro = System.getProperties();
       
       	pro.list(System.out);

	 //修改集合中的信息
	pro.setProperty("user.name","hehe");//从内存中做的修改

	pro.list(System.out);

 

再看一下它的基本使用

	public static void fun1()
	{
	   //只能存储键和值都是 String的
	   Properties  pro = new Properties();//创建了一个Map集合对象

	   pro.setProperty("name","lisi");
	   pro.setProperty("age","20");
	   pro.setProperty("sex","women");

	   Set<String> keys = pro.stringPropertyNames();//得到所有键的集合
	   Iterator<String> ite = keys.iterator();
	   while(ite.hasNext())
	    {
		String key = ite.next();
		String value = pro.getProperty(key);
		sop(key+"="+value);
	     }
       
        		//修改集合中的信息
		pro.setProperty("name","zhangsan");//在内存中的修改

		Set<String> jian = pro.stringPropertyNames();
		for(String jianjian:jian)
		{
			String value = pro.getProperty(jianjian);
			sop(jianjian+"="+value);
		}
	}

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


最后再看一个例子,“一个程序只能运行3次,超出三次给出要付费的提示”

import java.io.*;
import java.util.*;
class Demo
{
	public static void main(String[] args)throws IOException 
	{
		//记录一个程序的运行次数,大于5次时提示不能使用,去注册
        if(checkCount())
		{
			run();
		}
	}
	public static boolean checkCount()throws IOException
	{
		int num =1;//记录使用次数的变量
		//程序第一次被使用时,创建记录使用次数的文件
		File file = new File("count.properties");
		if(!file.exists())
			file.createNewFile();

		Properties pro = new Properties();
		//把文件中的信息存入集合
		FileInputStream fis =new FileInputStream("count.properties");
		pro.load(fis);

		//获取次数
		String value = pro.getProperty("count");
        	if(value!=null)
		{
			num = Integer.parseInt(value);
			if(num>5)
			{
				System.out.println("不能用了,去注册!");
				return false;
			}
		}
		num++;//次数加 1

		pro.setProperty("count",Integer.toString(num));

		//把集合中的信息存入文件
		FileOutputStream fos = new FileOutputStream("count.properties");
		pro.store(fos,"haha");

		fis.close();
        fos.close();

		return true;	
	}

	public static void run()
	{
		System.out.println("程序运行");
	}
}

 

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值