import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties; /**
* @Description:对输出的properties文件内容按key排序 */
public class OrderedProperties extends Properties { private static final long serialVersionUID = 1L; private List<Object> keyList = new ArrayList<Object>(); /**
* 默认构造方法
*/
public OrderedProperties() { } /**
* 从指定路径加载信息到Properties
*
* @param path
*/
public OrderedProperties(String path) {
try {
InputStream is = new FileInputStream(path);
this.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("指定文件不存在!");
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 重写put方法,按照property的存入顺序保存key到keyList,遇到重复的后者将覆盖前者。
*/
@Override
public synchronized Object put(Object key, Object value) {
this.removeKeyIfExists(key);
keyList.add(key);
return super.put(key, value);
} /**
* 重写remove方法,删除属性时清除keyList中对应的key。
*/
@Override
public synchronized Object remove(Object key) {
this.removeKeyIfExists(key);
return super.remove(key);
} /**
* keyList中存在指定的key时则将其删除
*/
private void removeKeyIfExists(Object key) {
keyList.remove(key);
} /**
* 获取Properties中key的有序集合
*
* @return
*/
public List<Object> getKeyList() {
return keyList;
} /**
* 保存Properties到指定文件,默认使用UTF-8编码
*
* @param path 指定文件路径
*/
public void store(String path) {
this.store(path, "UTF-8");
} /**
* 保存Properties到指定文件,并指定对应存放编码
*
* @param path 指定路径
* @param charset 文件编码
*/
public void store(String path, String charset) {
if (path != null && !"".equals(path)) {
try {
OutputStream os = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, charset));
this.store(bw, null);
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
throw new RuntimeException("存储路径不能为空!");
}
} /**
* 重写keys方法,返回根据keyList适配的Enumeration,且保持HashTable keys()方法的原有语义,
* 每次都调用返回一个新的Enumeration对象,且和之前的不产生冲突
*/
@Override
public synchronized Enumeration<Object> keys() {
return new EnumerationAdapter<Object>(keyList);
} /**
* List到Enumeration的适配器
*/
private static class EnumerationAdapter<T> implements Enumeration<T> {
private int index = 0;
private final List<T> list;
private final boolean isEmpty; public EnumerationAdapter(List<T> list) {
this.list = list;
this.isEmpty = list.isEmpty();
} public boolean hasMoreElements() {
// isEmpty的引入是为了更贴近HashTable原有的语义,在HashTable中添加元素前调用其keys()方法获得一个Enumeration的引用,
// 之后往HashTable中添加数据后,调用之前获取到的Enumeration的hasMoreElements()将返回false,但如果此时重新获取一个
// Enumeration的引用,则新Enumeration的hasMoreElements()将返回true,而且之后对HashTable数据的增、删、改都是可以在
// nextElement中获取到的。
return !isEmpty && index < list.size();
} public T nextElement() {
if (this.hasMoreElements()) {
return list.get(index++);
}
return null;
} } }

5 输出的properties文件按照key进行排序的更多相关文章

  1. 处理Properties文件中key包含空格的情况

    在这个互联网信息共享的时代,好处是一个问题的很多解决方案都可以从网络上得到,不好的一点就是很多人喜欢复制粘贴也不注明转载出处,不尊重别人的劳动成果,不假思索地把别人的原创复制到自己的博客然后发布,请大 ...

  2. 通过java.util.Properties类来读取.properties文件中key对应的value

    转:http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html

  3. java使用java.util.Properties读取properties文件的九种方法

    直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

  4. Spring 通过配置文件注入 properties文件

    当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...

  5. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  6. 170720、springboot编程之properties文件讲解

    但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华.在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华): (1) 在application.prope ...

  7. Eclipse的application.properties文件输出中文成unicode编码

    今天添application.properties时,无法输入中文,输入的中文直接变成了unicode的编码形式.原因是Eclipse的Spring Properties文件的默认编码为iso-885 ...

  8. Eclipse的.properties文件输出中文成unicode编码

    今天添加log4j.properties时,无法输入中文,输入的中文直接变成了unicode的编码形式.原因是Eclipse的.properties文件的默认编码为iso-8859-1. 选择Wind ...

  9. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

随机推荐

  1. poj2932 Coneology (扫描线)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Coneology Time Limit: 5000MS   Memory Lim ...

  2. 轻松完成WAP手机网站搭建

    用PHPCMS最新发布的V9搭建了PHPCMS研究中心网站(http://phpcms.org.cn)完成后,有用户提出手机访问的问题,于是着手搭建WAP无线站(wap.phpcms.org.cn). ...

  3. flask_socketio 使用范例

    最近看flask有关的文档.想security, sqlalchemy, jwt等等相关的插件都没有问题.唯独这个socketio, 跟着官方文档一起蒙, 如果不看github上面的范例还真不好懂. ...

  4. 关于开源中文搜索引擎架构coreseek中算法详解

     Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索.论坛/站内搜索.数据库搜索.文档/文献 ...

  5. Smartcard CA智能卡之调试

    Integrated Circuit Card  集成电路卡,也叫CA卡或智能卡,将一个微电子芯片嵌入符合ISO 7816标准的卡基内,做成卡片形式,也是一个嵌入式小系统.由CPU,ROM,RAM及E ...

  6. 如何将Icon转成Bitmap(对ICON的内部格式讲的比较清楚)

    最近工作中有个需求是将Icon转成带Alpha通道的Bitmap, 虽然网上有不少这方面的文章,但很多都是错的, 这里记录下,或许对后来人有用.   要实现这个功能,我们首先需要理解Icon的格式,我 ...

  7. poj2752 Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  8. eclipse3.7 安装maven插件与scm

    转自:http://blacksonny.iteye.com/blog/1900275 最近要使用maven进行开发,之前的eclipse3.7 使用一下两个地址安装好了插件,如下: maven插件 ...

  9. hdu 5422 Rikka with Graph(简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  10. BoneCP学习笔记

    什么是BoneCP BoneCP 是一个快速.免费而且开源的java数据库连接池(JDBC Pool)管理工具库.如果你曾经使用过C3P0或者DBCP,那你肯定知道上面这句话的意思:如果你没用过这些, ...