Java之Properties(配置文件类)

package com.shuhuadream.object;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息
 * 
 * Properties要注意的细节:
 * 1.如果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
 * 默认使用的是ISO8859-1码来进行编码存储,这时候会出现乱码。
 * 2.如果Properties中的内容发生变化,一定要重新使用Properties生产配置文件,否则配置文件信息不会发生变化
 * 
 * */
public class Demo02 {
	public static void main(String[] args) {
		createProperties();
		readProperties();
	}
	//读取配置文件的信息
	public static void readProperties(){
		//创建Properties对象
		Properties properties = new Properties();
		//加载配置文件信息到Properties中
		try {
			properties.load(new FileReader("d:\\demo\\persons.properties"));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//遍历Properties
		Set<Entry<Object,Object>> entrys = properties.entrySet();
		for (Entry<Object, Object> entry : entrys) {
			System.out.println("键:"+entry.getKey()+"值:"+entry.getValue());
		}
		
		//修改狗娃的密码
		properties.setProperty("狗娃", "007");
		try {
			properties.store(new FileWriter("d:\\demo\\persons.properties"), "hehe");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	//保存配置文件的信息
	public static void createProperties(){
		//创建Properties
		Properties properties = new Properties();
		properties.setProperty("狗娃", "123");
		properties.setProperty("狗剩", "234");
		properties.setProperty("铁蛋", "345");
		/*//遍历Properties
		Set<Entry<Object,Object>> entrys = properties.entrySet();
		for (Entry<Object, Object> entry : entrys) {
			System.out.println("键:"+entry.getKey()+"值:"+entry.getValue());
		}*/
		
		//使用Properties生成配置文件
		try {
			//properties.store(new FileOutputStream("d:\\demo\\persons.properties"), "haha");
			//第一个参数是一个输出流对象,第二个参数是使用一个描述这个配置文件的信息
			properties.store(new FileWriter("d:\\demo\\persons.properties"), "hehe");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*
 
 需求: 使用properties实现本软件只能 运行三次,超过了三次之后就提示购买正版,退jvm.
 
 */

public class Demo5 {
	
	public static void main(String[] args) throws IOException {
		File file = new File("F:\\count.properties");
		if(!file.exists()){
			//如果配置文件不存在,则创建该配置文件
			file.createNewFile();
		}
		
		//创建Properties对象。
		Properties properties = new Properties();
		//把配置文件的信息加载到properties中
		properties.load(new FileInputStream(file));
		FileOutputStream fileOutputStream = new FileOutputStream(file);

		int count = 0; //定义该变量是用于保存软件的运行次数的。
		//读取配置文件的运行次数
		String value = properties.getProperty("count");
		if(value!=null){
			count = Integer.parseInt(value);
		}
		
		//判断使用的次数是否已经达到了三次,
		if(count==3){
			System.out.println("你已经超出了试用次数,请购买正版软件!!");
			System.exit(0);
		}

		count++;
		System.out.println("你已经使用了本软件第"+count+"次");
		properties.setProperty("count",count+"");
		//使用Properties生成一个配置文件
		properties.store(fileOutputStream,"runtime");
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦客子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值