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<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 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<String> 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(key, true, false);if(comment != null && ! comment.equals("")){writeComments(bufferedWriter, comment);}bufferedWriter.write(key+"="+value);bufferedWriter.newLine();补充:软件开发 , Java ,
上一个:FreeMarker教程
下一个:Spring AOP记录系统日志
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值