今天想把输入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