Java生成配置文件Properties

package com.zyf.day22;

import java.io.FileNotFoundException;
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 (配置文件类):主要用于生产配置文件与读取配置文件的信息。
 * @author root
 *
 */
public class demo4 {
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//creatProperties();
		readProperties();
	}
	//读取配置文件的信息
	public static void readProperties() throws IOException{
	    //创建Properties对象
		Properties properties = new Properties();
		//加载配置文件信息到Properties中
		properties.load(new FileReader("c:\\person.properties"));
		//遍历Properties
		Set<Entry<Object,Object>> entrys = properties.entrySet();
		for(Entry<Object,Object> entry : entrys){
			System.out.println("键:" + entry.getKey() + "值:" + entry.getValue());
		}
		//修改张三密码
		properties.setProperty("张三", "007");
		properties.store(new FileWriter("c:\\person.properties"), "文件自述");
	}
	
	public static void creatProperties() throws IOException{
		//创建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生产配置文件
		//properties.store(new FileOutputStream("c:\\person.properties"), "文件自述");//第一个参数是一个输出流对象,第二个参数是使用一个字符描述这个配置文件信息
		properties.store(new FileWriter("c:\\person.properties"), "文件自述");
		
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值