1. 程式人生 > >第四十六講 I/O流——Properties集合

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

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("軟體執行");
	}

}