使用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. springboot集成jsp,访问jsp页面下载问题

    1.导入相关依赖     (存在jsp页面下载问题,可能是缺少tomcat-embed-jasper的依赖对jsp的支持) <parent> <groupId>org.spri ...

  2. 手写spark wordCount

    val conf: SparkConf = new SparkConf().setMaster(Local[*]).setAppName("wordCount") val sc=n ...

  3. 总结TestNg与JUnit的异同

    工作中一直用的是junit,近期稍微学习了一下TestNg,发现TestNg比java强大太多. TestNg简介 TestNg也是一套测试框架,它的灵感来源于Junit(java的单元测试框架)和N ...

  4. Saint John Festival Gym - 101128J (凸包二分)

    Problem J: Saint John Festival \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 给出\(n\)个大点,和\(m\ ...

  5. MATLAB画图笔记

    plot函数 plot(x,y)默认格式: 若x,y是向量,则它们必须具有相同的长度.函数将以x为横轴,绘制y. 若x,y都是矩阵,则它们必须具有相同的尺寸,plot函数将针对x的各列绘制y的每列.更 ...

  6. Vue的数据双向绑定原理——Object-defineProperty

    一.定义 ①方法会直接在一个对象上定义一个新属性,或者修改一个已经存在的属性, 并返回这个对象. ②vue.js的双向数据绑定就是通过Object.defineProperty方法实现的,俗称属性拦截 ...

  7. GIT与项目

    创建GitHub账号 本地Git仓库和GitHub仓库之间的传输是通过SSH加密的 step1:创建项目的SSH Key ssh-keygen -t rsa -C "your email@e ...

  8. mac 搭建Java Spring boot 环境(eclipse)

    安装 下载安装Springboot 安装完成后,创建项目 1. 2. 3. 完成创建!

  9. [Beta阶段]第十次Scrum Meeting

    Scrum Meeting博客目录 [Beta阶段]第十次Scrum Meeting 基本信息 名称 时间 地点 时长 第十次Scrum Meeting 19/05/16 大运村寝室6楼 30min ...

  10. java join()基本用法与说明解释

    join()方法的作用,是等待这个线程结束: 也就是说,t.join()方法阻塞调用此方法的线程(calling thread)进入 TIMED_WAITING 状态,直到线程t完成,此线程再继续: ...