Properties集合的介绍和使用

1.概述

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

 Map		 
   |---Hashtable
		|--Properties;

2.Properties集合特点:

1.该集合中的键和值都是字符串类型

2.集合中的数据可以保存到流中 或者从流中获取数据

3.通常该集合用于操作以键值对形式存在的配置文件

4.Properties集合是一个唯一和io流相结合的集合

5.可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储

6.可以使用Properties集合中的load方法,把硬盘中保存的文件(键值对),读取到集合中使用

3.properties文件的读取

下面以一段代码演示properties文件的读取操作:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Demo03 {
	public static void main(String[] args) {
		//Properties 格式文件的读取
		//创建输入流
		try (BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream("E:\\data.properties"))) {
			Properties props = new Properties();
			props.load(bis);//将“输入流”加载至Properties 集合对象中
			//根据key,获取value
			System.out.println(props.get("cn"));
			System.out.println(props.get("kr"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

4. properties文件的写出

下面以一段代码演示properties文件的写出操作:


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Demo04 {
	public static void main(String[] args) {
		//创建Properties集合对象,添加数据
		Properties pro;
		try {
			pro = new Properties();
			pro.put("F1", "1234");
			pro.put("F2", "2234");
			pro.put("F3", "2334");
			pro.put("F4", "2434");
			pro.put("F5", "5678");
           //创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
			try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\demo.properties"))) {
				//使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
				pro.store(bos, "just do it");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

注:第二个参数 String comments的作用是:注释,用来解释说明保存的文件是做什么用的,但是不能使用中文,因为涉及到了编码,默认是unicode编码,使用中文可能会产生乱码,一般使用空字符串来占位 。

得到的文本结果如下: 

 

5. 读取classpath资源文件

 从classpath读取文件就可以避免不同环境下文件路径不一致的问题:

 如果我们把default.properties文件放到classpath中,就不用关心它的实际存放路径;

 在classpath 中的资源文件,路径总是以/开头,我们先获取当前的Class对象,然后调用   getResourceAsStream()就可以直接从classpath读取任意的资源文件。

下面以一段代码演示读取classpath资源文件操作:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Properties读取当前包下的文件
* @Description
* @author tyq
* @version
* @date 2022-5-30 13:06:57
*
 */
public class Demo05 {
	public static void main(String[] args) {
//获取当前的Class对象,然后调用getResourceAsStream()就可以直接从classpath读取任意的资源文件
		try (InputStream in = Demo05.class.getResourceAsStream("/day23/demo.properties")) {
			//创建Properties集合类对象
			Properties  pro=new Properties();
			pro.load(in);//将输入流“加载”到Properties 集合类对象中
			System.out.println(pro);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

注意:本文中所有实现代码用到的流资源都直接声明在try块的一对括号内,在try块执行结束后会自动关闭流资源,无需调用流资源对象的close()方法手动关闭流资源。

 以上是近期学习的一点点小总结,如果对您有帮助,请留下您的关注和点赞,Thanks♪(・ω・)ノ

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值