设为首页 加入收藏

TOP

Java Properties类 按输入顺序输出,每条添加注释(一)
2014-11-24 12:02:17 来源: 作者: 【 】 浏览:105
Tags:Java Properties 输入 顺序 输出 添加 注释
今天想把输入Property类的信息,输出看一下,输出之后发现顺序是乱的,后来看了代码才明白怎么回事,于是决定自己瞎写一个,经测试还能用。望各位大神观摩指导。。。。
[java]
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 commentMap = new LinkedHashMap();
/**
* 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 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 the order of input.
* @param writer
* the writer
* @param comments
* the comments of this property file.
* @throws IOException
* exception.
*/
public void orderStore(Writer writer , String comments) throws IOException
{
BufferedWriter bufferedWriter = (writer instanceof BufferedWriter) (BufferedWriter)writer : new BufferedWriter(writer);
if (comments != null)
{
OutputOrderProperties.writeComments(bufferedWriter, comments);
}
bufferedWriter.write("#" + new Date().toString());
bufferedWriter.newLine();
bufferedWriter.newLine();
synchronized (this)
{
Iterator iterator = this.commentMap.keySet().iterator();
while(iterator.hasNext())
{
String key = iterator.next();
String value = this.getProperty(key);
String comment = this.commentMap.get(key);
key = saveConvert(key, true, false);
value = saveConvert(value, false, false);
key = saveConvert(
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇java关于集合的遍历与增强for循环.. 下一篇java关于可变参数的使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: