Properties

 * Properties: HashTable 的子类
 * 它具备map集合的特点,且内部存储的键值都是字符串
 * 是集合和IO技术相结合的集合容器
 * 特点: 可以用于键值对形式的配置文件
 * 
基本方法:
 * Object setProperty(String key, String value)  
 * 
 * String getProperty(String key)  
 * 
 * Set<String> stringPropertyNames() 返回此属性列表中的键集
 * 
 * void load(InputStream inStream)  从输入流中读取属性列表(键和元素对)。
 * 
 * void load(Reader reader)  
 * 
 * void list(PrintWriter out) 将属性列表输出到指定的输出流。 
 * 
 * void store(Writer writer, String comments)  
 * 需求:
 * 将info.txt文件中的键值数据存储到集合中进行操作
 * 
 * 1.用一个流和info.txt文件关联
 * 2.读一行数据,将该行数据用“=”进行切割(split)
 * 3.等号左边作为键,右边作为值,存入到Properties集合中
 * 


public static void method_1() throws Exception
	{
		BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
		Properties prop = new Properties();
		String line = null;
		while((line = bufr.readLine())!=null)
		{
			String[] str = line.split("=");
			prop.setProperty(str[0], str[1]);	
		}
		bufr.close();
		//写入到内存中,并不写入info.txt
		prop.setProperty("d", "dddd");
		System.out.println(prop.getProperty("d"));
		
	}
	
	public static void method_2() throws Exception
	{
		Properties prop = new Properties();
		FileReader fr = new FileReader("info.txt");
		//从输入流中读取属性列表(键和元素对)。
		prop.load(fr);


		//System.out.println(prop.getProperty("c"));


		//将属性列表输出到指定的输出流
		prop.list(System.out);
		
		//写入到内存中,并不写入info.txt
		prop.setProperty("d", "dddd");


		FileWriter fw = new FileWriter("info.txt");
		//将内存中的信息写入info.txt
		prop.store(fw, "zhushi");	
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值