Properties类的使用

Properties类

1.简介

Properties继承于Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串,其中键值对以等号分隔。Properties可保存在流中或从流中加载。属性列表中的每个键及其所对应的值都是字符串。Properties类是线程安全的:多个线程可以共享单个Properties对象而无需进行外部同步

文件类型通常是*.properties,格式:key=value

2.Properties类的重要方法

Properties 类存在于胞 Java.util 中,该类继承自 Hashtable

1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream  inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文
件中的所有键 - 值对。以供 getProperty (String  key) 来搜索。
3. setProperty ( String  key, String  value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。 
4. store ( OutputStream  out, String comments) ,   以适合使用load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素
对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

Properties定义了以下方法:

序号

方法描述

1

String getProperty(String key)
 
用指定的键在此属性列表中搜索属性。

2

String getProperty(String key, String defaultProperty)
用指定的键在属性列表中搜索属性。

3

void list(PrintStream streamOut)
 将属性列表输出到指定的输出流。

4

void list(PrintWriter streamOut)
将属性列表输出到指定的输出流。

5

void load(InputStream streamIn) throws IOException
 从输入流中读取属性列表(键和元素对)。

6

Enumeration propertyNames( )
按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。

7

Object setProperty(String key, String value)
 调用 Hashtable 的方法 put。

8

void store(OutputStream streamOut, String description)
 以适合使用  load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

3.Properties类的使用

a)加载外部properties文件,使配置和java代码解耦
public class SystemResourceUtil {
	private static Logger logger = LoggerFactory.getLogger(SystemResourceUtil.class);
	/**
	 * 用于存放加载的本地properties文件
	 */
	private final static Map<String, Properties> propertiesMap = new HashMap<String, Properties>();

        /**
	 * 获取本地资源文件
	 */
	public final static Properties getPropertisByName(String propertiesName) {
		Properties p = propertiesMap.get(propertiesName);
		// 内存中不存在  从本地中读取
		if (null == p) {
			p = FileUtil.getProperties(propertiesName);
			propertiesMap.put(propertiesName, p);
		}

		return p;
	}

        /**
	 * 获取配置文件公共方法
	 */
	public static Properties getProperties(String fileName) {
		// FileUtil fileUtil = new FileUtil();
		Properties p = new Properties();
		// InputStream in = FileUtil.class.getResourceAsStream(fileName);
		InputStream in = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(fileName);
		if (in == null) {
			return p;
		}
		try {
			p.load(in);
		} catch (IOException e) {
			logger.error("-- 读取配置文件失败", e);
		} finally {
			StreamUtil.closeStream(in);
		}
		return p;
	}

b)创建properties对象,设置属性值

        public void initProducer() {
		Properties p = new Properties();
		p.setProperty(MQCPConstant.PRODUCER_ID, producerId);
		MQCPProducer producer = null;
		try {
			// 获取MQCPProducer实例,producer是单实例的
			producer = MQCPFactory.createProducer(p);
			// 启动服务
			producer.start();
		} catch (MQCPException e) {
			return;
		}	
		PRODUCER_REGISTER.put(producerId, producer);
	}

4.获取文件流的3种方式和区别

方式1:InputStream in = this.getClass().getResourceAsStream("/cfs.properties");

方式2:InputStream in = this.getClass().getClassLoader().getResourceAsStream("cfs.properties");

方式3:InputStream in =Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE);

方式1的参数可以以/开头或者直接是名字。以/开头表示的是绝对路径,是从ClassPath的根路径开始查找,没有/直接名字,表示的是相对路径,则在该类所在的包里开始找这个文件,方式1本质调用的是方式2的方法,处理逻辑是如果有/则去掉/,否则直接把包名+参数前面

方式2默认从cClassPath根路径开始查找,不能以/开头

方式3得到的也是当前ClassPath的绝对URI路径。优先加载我们定义的类,加载不到的情况下再加载系统的。 这样的需求,是系统默认的父委托加载机制无法满足的,可以为当前线程指定相应的ClassLoader,然后用get的方式来获取。通用性更好

其中,bin是本项目的classpath。所有的Java源文件编译后的.class文件复制到这个目录中。

注:以下获取到的是相同的

  com.explorers.Test.class.getResourceAsStream("abc.jpg")=com.explorers.Test.class.getResourceAsStream("/com/explorers/abc.jpg")=ClassLoader.getResourceAsStream("com/explorers/abc.jpg")


猜你喜欢

转载自blog.csdn.net/weixin_37598682/article/details/79910283