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. url编码方法(暂时知道是什么

    var a="https://i.cnblogs.com/EditPosts.aspx?opt=1" encodeURI(a); encodeURIComponent(); dec ...

  2. Java系列--第八篇 基于Maven的SSME之定时邮件发送

    关于ssme这个我的小示例项目,想做到麻雀虽小,五脏俱全,看到很多一些web都有定时发送邮件的功能,想我ssme也加入一下这种功能,经查询相关文档,发现spring本身自带了一个调度器quartz,下 ...

  3. nodejs安装过程及视频地址

    说实话在安装的过程中遇到了很多问题,包括npm install connect出错,主要是我之前安装了0.10版本之后安装4.X版本造成的,后面卸载没有卸载完全造成的,后面也就好了,网上说了很多重新设 ...

  4. 如何在symfony 控制器里面创建soap web service

    通过一些工具将一个控制器设置成一个soap服务将会非常简单.首先,你必须安装了php soap扩展.由于php soap扩展现在不能生成wsdl,你要么自己从头开始创建要模使用第三方生成器. php中 ...

  5. Lintcode--002(两个字符串是变位词)

    写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 您在真实的面试中是否遇到过这个题?     样例 给出 s = "abcd", ...

  6. angularJs项目实战!01:模块划分和目录组织

    近日来我有幸主导了一个典型的web app开发.该项目从产品层次来说是个典型的CRUD应用,故而我毫不犹豫地采用了grunt + boilerplate + angularjs + bootstrap ...

  7. windows设备驱动安装指南

    高观点下的设备驱动安装(overview) 一.windows是怎样安装设备的? 第一步:新设备的识别 在给一个新设备安装驱动之前,总线或集线器(hub)驱动会为连接到PC上的设备分配一个硬件ID(h ...

  8. Row Cache Objects

    This latch comes into play when user processes are attempting to access or update the cached data di ...

  9. OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程

    OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程 OpenJTAG与JLink的区别比较: 相同点:都同时具备USB转JTAG.USB转串口功能 差别: 1. 操作系统: O ...

  10. Hadoop开发遇到的问题之reduce卡住

    遇到的问题描述:在hadoop上面执行程序,程序运行之后能够正常执行.一切似乎都是正常的,然而过了一段时间之后程序便开始阻塞直到程序超时退出(如下). 14/08/19 21:17:51 INFO m ...