使用java.util.Properties提供的类,读取properties文件的时候,读出来的是乱序的

如下边的情况

import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties; public class PropertyDemo { public static List<String> old = Arrays.asList("自来水","纯净水", "矿泉水","山泉水" ); public static void main(String[] args) {
initType();
} public static void initType() { String path = System.getProperty("user.dir").replaceAll("\\\\", "/");
path = path + "/waterType.properties";
File file = new File(path);
Properties properties = new Properties();
if (!file.exists()) {
try {
FileOutputStream oFile = new FileOutputStream(path, true);
int i = 0;
int len = old.size();
for (; i < len; i++) {
properties.setProperty(String.valueOf(i + 1), old.get(i));
}
properties.store(oFile, "");
oFile.close();
} catch (IOException e) {
e.printStackTrace();
} }
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
properties.load(in);
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} //遍历
Enumeration<?> e= properties.propertyNames();
while (e.hasMoreElements()){
String key = (String) e.nextElement();
String value = properties.getProperty(key);
System.out.println(key + "=" + value);
} }
}

输出

4=山泉水
3=矿泉水
2=纯净水
1=自来水

保存到文件的顺序也是如此

那如果想是有序的,怎么办呢?

自定义一个Properties 类

import java.util.*;

public class OrderedProperties extends Properties {

    private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();

    public Enumeration<Object> keys() {
return Collections.<Object>enumeration(keys);
} public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
} public Set<Object> keySet() {
return keys;
} public Set<String> stringPropertyNames() {
Set<String> set = new LinkedHashSet<String>();
for (Object key : this.keys) {
set.add((String) key);
}
return set;
} }

使用

import java.io.*;
import java.util.*; public class PropertyDemo { public static List<String> old = Arrays.asList("自来水","纯净水", "矿泉水","山泉水" ); public static void main(String[] args) {
initType();
} public static void initType() { String path = System.getProperty("user.dir").replaceAll("\\\\", "/");
path = path + "/waterType.properties";
File file = new File(path);
Properties properties = new OrderedProperties();
if (!file.exists()) {
try {
FileOutputStream oFile = new FileOutputStream(path, true);
int i = 0;
int len = old.size();
for (; i < len; i++) {
properties.setProperty(String.valueOf(i + 1), old.get(i));
}
properties.store(oFile, "");
oFile.close();
} catch (IOException e) {
e.printStackTrace();
} }
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
properties.load(in);
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} //遍历
Set<String> e = properties.stringPropertyNames();
for (String one : e) {
String key = one;
String value = properties.getProperty(one);
System.out.println(key + "=" + value);
} }
}

输出

1=自来水
2=纯净水
3=矿泉水
4=山泉水

保存到文件的顺序也是如此

多少迷茫,曾经在幽幽暗暗、反反复复中追问,才知道平平淡淡从从容容才是真。再回首恍然如梦,再回首我心依旧

Properties的有序读写的更多相关文章

  1. Java程序员的日常—— Properties文件的读写

    在日常的Java程序开发中,Properties文件的读写是很常用的.经常有开发系统通过properties文件来当做配置文件,方便用户对系统参数进行调整. 那么本片就来简单的介绍下,如何使用Prop ...

  2. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  3. K:java中properties文件的读写

    Properties类与.properties文件:   Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...

  4. 实现对properties文件的有序读写

    最近遇到一项需求,要求把properties文件中的内容读取出来供用户修改,修改完后需要再重新保存到properties文件中.很简单的需求吧,可问题是Properties是继承自HashTable的 ...

  5. JSP+Java+properties+FileInputStream文件读写,JSP页面读取properties文件

    String realPath = request.getRealPath("WEB-INF/classes/com/properties/devicetype.properties&quo ...

  6. JAVA Properties配置文件的读写

    通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的.在java中,这种文件的内容以键值对<key,value>存储,通常以“=” ...

  7. Java读写资源文件类Properties

    Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置.  注 ...

  8. Properties读写资源文件

    Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...

  9. Java读写配置文件——Properties类的简要使用笔记

    任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...

随机推荐

  1. python基础语法3 整形,浮点,字符串,列表

    整形: ========================基本方法===========================用途: 一般用于定义整数:年龄.身份证号.qq号定义方式:age = 18 # a ...

  2. Helm 安装Nginx Ingress

    为了便于将集群中的服务暴露到集群外部,需要使用Ingress.接下来使用Helm将Nginx Ingress部署到Kubernetes上. Nginx Ingress Controller被部署在Ku ...

  3. MySQL备份python代码

    import os, time, pymysql, shutil from apscheduler.schedulers.blocking import BlockingScheduler # 定时任 ...

  4. EJS的个人总结

    什么是模板引擎? 用于Web开发的模板引擎是为了使用用户界面与业务数据(内容)分离而产生的,使用模板语法编写的模板代码通常放在具有特的格式的文档中,经过模板引擎编译之后就会生成一个标准的HTML文档. ...

  5. RookeyFrame 一些心得 或者 调试技巧等

    因为没有依赖具体的实现层,类库的输出路径又没有设置在web层的bin目录,所以每次都要拷贝实现层的DLL过去,有时候拷贝过去了还是没有反应,估计是缓存什么的吧, 解决:先那几个web层bin目录的 D ...

  6. luogu P1447 [NOI2010]能量采集 欧拉反演

    题面 题目要我们求的东西可以化为: \[\sum_{i=1}^{n}\sum_{j=1}^{m}2*gcd(i,j)-1\] \[-nm+2\sum_{i=1}^{n}\sum_{j=1}^{m}gc ...

  7. Fluent——UDF监测指定点的物理量

    Fluent版本:19.0 Fluent当中提供了监测某一点物理量随迭代次数或者随时间变化的功能,下面我们就介绍如何在UDF当中实现相同的功能,并且UDF更加灵活,通过UDF的方式我们在知道某点运动规 ...

  8. WINDOWS 命令行调用SAS代码 并指定输出路径 示例

    ECHO "设置SAS.EXE 路径" SET PATH=D:\Program Files\SASHome\SASFoundation\9.4\SAS.EXE echo " ...

  9. stream_context_create解析

    (PHP 4 >= 4.3.0, PHP 5, PHP 7) stream_context_create — 创建资源流上下文 说明¶ stream_context_create ([ arra ...

  10. php error_reporting()关闭报错

    错误报告级别:指定了在什么情况下,脚本代码中的错误(这里的错误是广义的错误,包括E_NOTICE注意.E_WARNING警告.E_ERROR致命错误等)会以错误报告的形式输出. 一.常用设置说明 er ...