黑马程序员:Properties对象

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

Properties    是hashtable的子类。就是说它具备map集合的特点,可以存储字符串的键值对。

                    是集合中和IO技术相结合的集合容器。

该对象的特点:可以用于键值对形式的配置文件。那么在加载数据时,需要有固定格式:键=值。


import java.io.*;
import java.util.*;
class PropertiesDemo  
{
	public static void main(String[] args) throws IOException
	{
		loadDemo();
	}
	
	public static void loadDemo()throws IOException
	{
		Properties prop = new Properties();
		FileReader fr = new FileReader("info.txt");
		prop.load(fr);
		prop.setProperty("lisi","22");
		FileWriter fw = new FileWriter("info.txt");
		prop.store(fw,"change");
		prop.list(System.out);
		fr.close();
		fw.close();
	}
	public static void method()throws IOException
	{
		BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
		String len = null;
		Properties prop = new Properties();
		
		while((len = bufr.readLine())!=null)
		{
			String[] str = len.split("=");
			prop.setProperty(str[0],str[1]);
			
		}
		sop(prop);
		bufr.close();
	}
	public static void setAndGet()
	{
		Properties prop = new Properties();
		prop.setProperty("zhangsan","30");
		prop.setProperty("lisi","35");
		sop(prop);
		String value = prop.getProperty("lisi");
		sop(value);
		Set<String> names = prop.stringPropertyNames();
		for(String s : names)
		{
			sop(s+":"+prop.getProperty(s));
		}
	}


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

----------------------- android培训java培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net/heima
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值