Java Properties类 按输入顺序输出,每条添加注释

本文介绍如何使用Java Properties类按照输入顺序输出属性,并为每条属性添加注释。作者发现标准Properties类在输出时顺序不固定,于是自定义了一个解决方案,经过测试能够实现预期效果。
摘要由CSDN通过智能技术生成

    今天想把输入Property类的信息,输出看一下,输出之后发现顺序是乱的,后来看了代码才明白怎么回事,于是决定自己瞎写一个,经测试还能用。望各位大神观摩指导。。。。

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Properties;

/**
 * This class is to provide function output properties by input order and add comment to each property.
 * @author zhengfan1
 */
public class OutputOrderProperties extends Properties
{

	private LinkedHashMap<String, String> commentMap = new LinkedHashMap<String,String>();
	
	/**
	 * Version ID
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor.
	 */
	public OutputOrderProperties()
	{
		super();
	}
	
	/**
	 * Constructor.
	 * @param properties
	 * 		the java propertis.
	 */
	public OutputOrderProperties(Properties properties)
	{
		super(properties);
		//Initialize the comment.
		Iterator<Object> iterator = properties.keySet().iterator();
		while(iterator.hasNext())
		{
			Object key = iterator.next();
			this.commentMap.put((String) key, null);
		}
	}
	
	/**
	 * Add comment to a property.
	 * @param key
	 * 		the key of the property.
	 * @param comment
	 * 		the comment of the property.
	 * @return
	 * 		true => add it
	 * 		false => don't have this key.
	 */
	public boolean addComment(String key , String comment)
	{
		if(this.contains(key))
		{
			this.commentMap.put(key, comment);
			return true;
		}
		return false;
	}
	
	/**
	 * To set property.
	 * @param key
	 * 		the key of property.
	 * @param value
	 * 		the value of property.
	 * @param comment
	 * 		the comment of property.
	 */
	public void setP(String key , String value , String comment)
	{
		this.commentMap.put(key, comment);
		this.setProperty(key, value);
	}
	
	/**
	 * To output according to th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值