学习笔记之JavaSE(44)--IO流6

本文详细介绍了Java中的Properties类,包括其特点、应用场景以及如何利用Properties类和IO技术实现程序运行次数的记录与限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习的内容是Properties类


Properties类已经见过两次了,实际上它是一个属性集合主要用于结合IO技术操作配置文件。Properties类的特点是:

  • 集合中的键值对都是字符串
  • 集合中的数据可以保存到流中,或者从流中获取
  • 可以被持久化(也就是持久化到配置文件中)
顺便说一下配置文件:配置文件在实际开发中十分重要,用于配置应用程序所需的环境信息。配置简单信息通常使用.ini或者.properties格式的配置文件(例如System.ini和Tomcat中的catalina.properties),这种配置文件内部存储的是“键=值”形式的配置信息;而配置复杂环境信息就要使用XML配置文件(框架中会大量使用这种配置文件!),这种配置文件内部存储的是标签形式的配置信息

关于Properties类的常用操作如下例:
public class Test85 {

	public static void main(String[] args) throws IOException {

		propertiesTest_1();
		propertiesTest_2();
		propertiesTest_3();
		propertiesTest_4();
		propertiesTest_5();
	}

	//对已有配置文件中的信息进行修改
	private static void propertiesTest_5() throws IOException {
		
		Properties prop = new Properties();
		File file = new File("info.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		prop.load(new FileInputStream(file));
		prop.setProperty("1", "k");
		prop.list(System.out);
		prop.store(new FileOutputStream(file), "modify");
		prop.list(System.out);
	}

	//Properties类的load()方法
	private static void propertiesTest_4() throws IOException {
		
		//集合中的数据来自于一个文件,需要关联输入流,注意必须保证该文件中的数据是键值对
		Properties prop = new Properties();
		prop.load(new FileInputStream("info.txt"));
		prop.list(System.out);
	}

	//Properties类的store()方法
	private static void propertiesTest_3() throws IOException{
		
		//想要将这些集合中的键值对信息持久化存储到文件中,需要关联输出流
		Properties prop = new Properties();
		prop.setProperty("1", "a");
		prop.setProperty("2", "b");
		prop.setProperty("3", "c");
		prop.setProperty("4", "e");
		prop.setProperty("4", "d");
		prop.store(new FileOutputStream("info.txt"), "int+abc");// 文件描述最好不要使用中文
	}

	// Properties类的list()方法
	private static void propertiesTest_2() {

		Properties prop = System.getProperties();
		prop.list(System.out);// 将出行列表输出到指定输出流(该方法只是用来调试)
	}

	// Properties类基本操作
	private static void propertiesTest_1() {

		Properties prop = new Properties();
		prop.setProperty("1", "a");
		prop.setProperty("2", "b");
		prop.setProperty("3", "c");
		prop.setProperty("4", "e");
		prop.setProperty("4", "d");
		Set<String> keySet = prop.stringPropertyNames();
		for (String key : keySet) {
			String value = prop.getProperty(key);
			System.out.println(key + ":" + value);
		}
	}

}

若希望使用Properties类和IO技术实现功能:获取程序运行的次数,如果超过5次,给出使用次数已到请注册的提示,并不再运行该程序。由于程序运行结束,作为计数器的变量将会消失,下一次启动程序将会重新初始化计数器,所以要将程序运行次数持久化到配置文件中,下例使用.properties格式的配置文件完成上述功能:
public class Test86 {

	public static void main(String[] args) {

		getAppCount();
	}

	private static void getAppCount() {

		// 创建配置文件
		File confile = new File("count.properties");// 注意Windows常用.ini格式的配置文件
													// Java中则是.properties格式的配置文件
		try {
			if (!confile.exists()) {
				confile.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 创建Properties集合
		Properties prop = new Properties();

		// 创建输入流,读取配置文件中的内容
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(confile);
			prop.load(fis);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// 从集合中通过键获取次数
		String value = prop.getProperty("time");
		int count = 0;// 计数器
		if (value != null) {
			count = Integer.parseInt(value);
			if (count >= 5) {
				throw new RuntimeException("使用次数已到,请注册!");
			}
		}
		count++;
		System.out.println("第"+count+"次运行程序");

		// 将改变后的次数重新存储到集合中
		prop.setProperty("time", Integer.toString(count));

		// 将集合中的内容写到配置文件中
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(confile);
			prop.store(fos, "jishuqi");;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值