Properties学习

package cn.zen.io.properties;

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

import org.junit.Test;

public class PropertiesEx {

	@Test
	public void method_1() {
		Properties prop = new Properties();
		prop.setProperty("ZhangSan", "100");
		prop.setProperty("LiSi", "120");
		prop.setProperty("WangWu", "99");

		// System.out.println(prop.getProperty("LiSi"));
		Set<String> nameSet = prop.stringPropertyNames();
		for (String name : nameSet) {
			System.out.println(name + "->" + prop.getProperty(name));
		}
	}

	/**
	 * 将info.ini文件中键值数据存到Properties集合中进行操作 步骤: 1:一个IO流和文件关联info.ini文件关联
	 * 2:读取一行数据,根据=进行数据切割 3:左边为键,右边为值 存到Properties集合中去
	 */
	@Test
	public void method_2() throws IOException {
		// 由于源是纯文本文件 所以用FileReader
		File initFile = new File("D:\\StudyIo", "info.ini");
		if (!initFile.exists())
			System.out.println("该文件不存在!");
		BufferedReader bufr = new BufferedReader(new FileReader(initFile));

		Properties prop = new Properties();

		String buf = null;
		while ((buf = bufr.readLine()) != null) {
			// System.out.println(buf);
			String[] strArr = buf.split("=");
			// System.out.println(strArr[0]+"->"+strArr[1]);
			prop.setProperty(strArr[0], strArr[1]);
		}
		System.out.println(prop);
	}

	@Test
	public void method_3() throws FileNotFoundException, IOException {
		Properties prop = new Properties();

		File initFile = new File("D:\\StudyIo", "info.ini");
		if (!initFile.exists())
			System.out.println("该文件不存在!");

		FileInputStream fis = new FileInputStream(initFile);

		prop.load(fis);

		prop.setProperty("color", "bule");
		// System.out.println(prop);
		// prop.list(System.out);
		FileOutputStream fos = new FileOutputStream(initFile);
		prop.store(fos, "color change red to bule");

		fos.close();
		fis.close();
	}
}
—————————————————————————————————————————————————————————————————————————————

package cn.zen.io.properties;

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

public class ImitateRegistTip {
	public static void main(String[] args) throws IOException {
		Properties prop = new Properties();
		File initFile = new File("D:\\StudyIo", "info.property");

		if (!(initFile.exists())) {
			if (!(initFile.createNewFile())) {
				System.out.println("创建" + initFile.getName() + "失败!");
				return;
			}
		}
		FileInputStream fis = new FileInputStream(initFile);
		prop.load(fis);

		int cnt = 0;
		String cntVal = prop.getProperty("accessCnt");
		if (cntVal != null) {
			cnt = Integer.parseInt(cntVal);
			if (cnt >= 3) {
				System.out.println("试用次数已过,如需再次使用请注册激活!");
			}
		}
		cnt++;
		prop.setProperty("accessCnt", cnt + "");

		FileOutputStream fos = new FileOutputStream(initFile);
		String strMsg = "this is " + cnt + " times access! change accessCnt from "
				+ (cnt - 1) + " to " + cnt;
		prop.store(fos, strMsg);

		fos.close();
		fis.close();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值