第四十六讲 I/O流——Properties集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yerenyuan_pku/article/details/84575429

Properties概述

Properties是HashTable的子类,也就是说它具备Map集合的特点;而且它里面存储的键值对都是字符串,没有泛型定义;最后它是一个可以和IO流相结合使用的属性集合类。

Properties常用操作方法

Properties基本的存和取

方法 描述
public Object setProperty(String key,String value) 调用Hashtable的方法put方法,即设置元素
public String getProperty(String key) 用指定的键在此属性列表中搜索属性,即获取元素
public Set<String> stringPropertyNames() 返回此属性列表中的键集
package cn.liayun.properties;

import java.util.Properties;
import java.util.Set;

public class PropertiesSimple {

	public static void main(String[] args) {
		methodDemo();
	}
	
	public static void methodDemo() {
		//Properties的基本存和取
		//1,创建一个Properties
		Properties prop = new Properties();
		prop.setProperty("zhangsan", "20");
		prop.setProperty("lisi", "23");
		prop.setProperty("wangwu", "21");
		
//		prop.list(System.out);//一般用于调试,所以少用。
		
		Set<String> set = prop.stringPropertyNames();
		for (String name : set) {
			String value = prop.getProperty(name);
			System.out.println(name + "...." + value);
		}
	}

}

Properties和IO流的结合使用

方法 描述
public void load(InputStream inStream) 把文件中的数据读取到集合中
public void store(OutputStream out, String comments) 把集合中的数据存储到文件。comments为属性列表的描述
package cn.liayun.properties;

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

public class PropertiesDemo {

	public static void main(String[] args) throws IOException {
		/*
		 * 演示Properties的特有方法
		 */
//		methodDemo2();
		methodDemo3();
	}

	public static void methodDemo3() throws IOException {
		File configFile = new File("tempfile\\info.properties");
		
		//读取流中的数据
		Properties prop = new Properties();
		//定义读取流和数据文件关联
		FileInputStream fis = new FileInputStream(configFile);
		prop.load(fis);
		
		prop.setProperty("zhangsan", "12");
		
		//要将改完的数据重新持久化
		FileOutputStream fos = new FileOutputStream(configFile);
		prop.store(fos, "");
		
//		prop.list(System.out);
		fos.close();
		fis.close();
	}

	public static void methodDemo2() throws IOException {
		Properties prop = new Properties();
		prop.setProperty("zhangsan", "20");
		prop.setProperty("lisi", "23");
		prop.setProperty("wangwu", "21");
		
		//将集合中的数据持久化存储到设备上
		//需要输出流对象
		FileOutputStream fos = new FileOutputStream("tempfile\\info.properties");
		//使用prop的store方法
		prop.store(fos, "my demo, person info");
		fos.close();
	}

}

练习

编写一个小程序,记录程序运行的次数,满足5次后,给出提示,试用次数已到,请注册!

分析:

  1. 需要计数器;
  2. 计数器的值,生命周期要比应用程序的生命周期要长,需要对计数器的值进行持久化。count = 1,里面存储的应该是键值方式,Map集合,要和设备上的数据关联,需要IO技术。集合 + IO = Properties。
package cn.liayun.test;

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

public class Test {

	public static void main(String[] args) throws IOException {
		/*
		 * 需求:定义一个功能,记录程序运行的次数,满足5次后,给出提示,试用次数已到,请注册!
		 * 思路:
		 * 1,需要计数器
		 * 2,计数器的值,生命周期要比应用程序的生命周期要长,需要对计数器的值进行持久化
		 *    count = 1,里面存储的应该是键值方式,Map集合,要和设备上的数据关联,需要IO技术。
		 *    集合 + IO = Properties。
		 */
		boolean b = checkCount();
		if (b) {
			run();
		}
	}
	
	public static boolean checkCount() throws IOException {
		boolean isRun = true;
		
		//1,将配置文件封装成File对象,因为要判断文件是否存在
		File configFile = new File("tempfile\\count.properties");
		if (!configFile.exists()) {//如果不存在,就创建
			configFile.createNewFile();
		}
		
		int count = 0;//记录住每次存储的次数
		
		Properties prop = new Properties();//用于存储配置文件中的数据
		//2,定义流对象
		FileInputStream fis = new FileInputStream(configFile);
		
		//3,将流中的数据加载到集合中
		prop.load(fis);
		
		//4,获取键对应的次数
		String value = prop.getProperty("count");
		if (value != null) {
			count = Integer.parseInt(value);
			if (count >= 5) {
				System.out.println("使用次数已到,请注册,给钱!");
				isRun = false;
			}
		}
		count++;//对取出的次数进行自增
		//将键count,和自增后的值重新存储到集合中
		prop.setProperty("count", Integer.toString(count));
		//将集合中的数据存储到配置文件中
		FileOutputStream fos = new FileOutputStream(configFile);
		prop.store(fos, "");
		
		fos.close();
		fis.close();
		return isRun;
	}

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

}

猜你喜欢

转载自blog.csdn.net/yerenyuan_pku/article/details/84575429