Properties类


一、概述

PropertiesJava重要的一个类, 继承自Hashtable。 主要用于读取Java中的配置文件,方便程序员或用户来修改,实现应用程序数据共享。Java中的配置文件常为.properties文件,格式为文本文件,文本的内容格式是“键=值”形式,文本注释信息用“#”标识。

二、主要方法:

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 (),清除所有装载的 键 - 值对。该方法在基类中提供。

 

 三、示例

1如何将流中的数据存储到集合中

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest
{
	public static void main(String[] args)throws IOException
	{
		//setAndGet();
		method_1();
		//loadDemo();
	}
	//运用Properties自带的load方法将数据加载到集合中
	public static void  loadDemo() throws IOException
	{
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream("C:\\info.txt");
		//将流中的数据加载到集合中
		prop.load(fis);
		//修改其中的数据
		prop.setProperty("zhangsan", "99");
		//修改后要将源文件中的数据已更改
		FileOutputStream fos = new FileOutputStream("C:\\info.txt");
		prop.store(fos, "haha");
		//修改后要将源文件中的数据已更改
		prop.list(System.out); 
		fos.close();
		fis.close();
	}
	 
 	//设置和获取元素
	public static void  setAndGet()
	{
		Properties prop = new Properties();
		prop.setProperty("zhangsan", "20");
		prop.setProperty("lisin", "44");
		
		String value = prop.getProperty("lisin");
		//System.out.println(value);
		//通过Properties的stringPropertyNames()获取键值
		Set<String> names = prop.stringPropertyNames();
		for (String s : names)
		{
			System.out.println(s+":" + prop.getProperty(s));
		}
	}
}


2、用于记录应用程序运行次数,如果次数到后,给出注册提示

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class RunCount
{
	public static void main(String[] args) throws IOException
	{
		Properties prop = new Properties();
		//将文件封装成File对象,通过File对象进行判断,文件读取流就不会报异常
		File file = new File("count.ini");
		if (!file.exists())
		{
			file.createNewFile();
		}
		
		//文件读取流
		FileInputStream fis = new FileInputStream(file);
		//将流中的数据加载到集合中
		prop.load(fis);
		//获取次数的值,如果此时大于5次,提示用户注册
		int count = 0;
		String value = prop.getProperty("time");
		
		if (value != null)
		{
			count = Integer.parseInt(value);
			if (count >= 5)
			{
				System.out.println("您好,使用次数已过期,请注册!");
				return;
			}
		}
		count++;
		//将新的键值写入文件
		prop.setProperty("time", count+"");
		FileOutputStream fos = new FileOutputStream(file);
		prop.store(fos, "");
		fos.close();
		fis.close();
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值