Java学习笔记之IO(十四):Properties配置文件类

package com.io.g_sequence;

import java.io.File;
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;

import org.junit.Test;

/*

Properties:配置文件类,主要用于产生配置文件和读取配置文件的信息;

	Properties是属于Map集合体系的类; 因为该类继承于Hashtable,所以可以使用put()和putAll()方法,
	但是不建议使用;因为这两个方法可以添加任意类型的数据,而Properties用来操作的配置文件中都是字符串数据,
	所以建议使用setProperty(String, String)方法;
	
	Properties注意事项:
	1、如果配置文件的信息使用了中文,那么在使用 store() 方法生成配置文件的时候,只能使用字符流方式;如果使用
		字节流生成配置文件,默认使用的是ISO8859-1进行编码存储,会出现乱码;
	2、如果Properties中的内容发生了变化,要重新调用store()方法将数据更新到配置文件中;

 */

public class Demo4 {

	// Properties属于Map集合体系的基本遍历方式
	@Test
	public void test1(){
		// 创建Properties对象
		Properties prop = new Properties();
		prop.setProperty("张三", "123");
		prop.setProperty("李四", "234");
		prop.setProperty("王五", "345");
		
		// 遍历Properties
		Set<Entry<Object, Object>> entrySet = prop.entrySet();
		for (Entry<Object, Object> entry : entrySet){
			System.out.println(entry.getKey() + "---" + entry.getValue());
		}
	}
	
	// 使用 Properties 生成配置文件,并使用字节流方法将数据写入到配置文件中
	@Test
	public void test2() throws IOException{
		// 创建Properties对象
		Properties prop = new Properties();
		prop.setProperty("Jack", "123");
		prop.setProperty("Lucy", "234");
		prop.setProperty("Lily", "345");
		
		// 找到目标配置文件
		File file = new File("D:\\user.properties");
		// 创建数据输出流对象
		FileOutputStream out = new FileOutputStream(file);
		
		// 生成配置文件;参数1:配置文件的输出流对象;参数2:使用一个字符串描述配置文件的信息;
		prop.store(out, "user");
	}
	
	/*
	使用 Properties 生成配置文件,并使用字节流将中文数据写入到配置文件中,结果为:
		#user
		#Wed Oct 11 16:48:08 CST 2017
		\u738B\u4E94=345
		\u5F20\u4E09=123
		\u674E\u56DB=234
	查看 store() 方法的源码可以看出,该方法默认使用的是 ISO8859-1 编码方式,该编码方式不支持中文;
	解决办法:可以使用字符流写入中文数据;	
	*/
	@Test
	public void test3() throws IOException{
		// 创建Properties对象
		Properties prop = new Properties();
		prop.setProperty("张三", "123");
		prop.setProperty("李四", "234");
		prop.setProperty("王五", "345");
		
		// 找到目标配置文件
		File file = new File("D:\\user.properties");
		// 创建数据输出字节流对象
		FileOutputStream out = new FileOutputStream(file);
		
		// 生成配置文件; 参数1:配置文件的输出字节流对象; 参数2:使用一个字符串描述配置文件的信息;
		prop.store(out, "user");
	}
	
	// 使用 字符流 向配置文件中写入 中文
	@Test
	public void test4() throws IOException{
		// 创建Properties对象
		Properties prop = new Properties();
		prop.setProperty("张三", "123");
		prop.setProperty("李四", "234");
		prop.setProperty("王五", "345");
		
		// 找到目标配置文件
		File file = new File("D:\\user.properties");
		// 创建数据输出字节流对象
		FileWriter writer = new FileWriter(file);
		
		// 生成配置文件;参数1:配置文件的输出字符流对象;参数2:使用一个字符串描述配置文件的信息;
		prop.store(writer, "user");
	}
	
	// 读取配置文件数据;由于Properties是一个容器,所以可以直接把数据加载到该容器中;
	@Test
	public void test5() throws IOException{
		// 创建Properties对象
		Properties prop = new Properties();
		
		// 找到目标文件
		File file = new File("D:\\user.properties");
		// 建立数据输入字符流
		FileReader reader = new FileReader(file);
		// 加载配置文件到Properties容器中;参数是一个输入流对象;
		prop.load(reader);
		
		// 如果知道键,可以直接根据键获取值
		System.out.println("张三:" + prop.getProperty("张三"));;
		System.out.println("李四:" + prop.getProperty("李四"));;
		System.out.println("王五:" + prop.getProperty("王五"));;
		
		// 遍历集合容器
		Set<Entry<Object, Object>> entrys = prop.entrySet();
		for (Entry<Object, Object> entry : entrys){
			System.out.println(entry.getKey() + "---" + entry.getValue());
		}
		
		// 修改配置文件数据;此处修改的只是集合容器中的数据,并没有更新到配置文件中;
		prop.setProperty("张三", "007");
		// 将修改之后的集合容器中的数据重新写入配置文件中
		prop.store(new FileWriter(file), "user");
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_29331365/article/details/78210878