Properties的有序读写
使用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的有序读写的更多相关文章
- Java程序员的日常—— Properties文件的读写
在日常的Java程序开发中,Properties文件的读写是很常用的.经常有开发系统通过properties文件来当做配置文件,方便用户对系统参数进行调整. 那么本片就来简单的介绍下,如何使用Prop ...
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
- K:java中properties文件的读写
Properties类与.properties文件: Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...
- 实现对properties文件的有序读写
最近遇到一项需求,要求把properties文件中的内容读取出来供用户修改,修改完后需要再重新保存到properties文件中.很简单的需求吧,可问题是Properties是继承自HashTable的 ...
- JSP+Java+properties+FileInputStream文件读写,JSP页面读取properties文件
String realPath = request.getRealPath("WEB-INF/classes/com/properties/devicetype.properties&quo ...
- JAVA Properties配置文件的读写
通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的.在java中,这种文件的内容以键值对<key,value>存储,通常以“=” ...
- Java读写资源文件类Properties
Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注 ...
- Properties读写资源文件
Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...
- Java读写配置文件——Properties类的简要使用笔记
任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...
随机推荐
- flutter 从创建到渲染的大体流程
从创建到渲染的大体流程是:根据Widget生成Element,然后创建相应的RenderObject并关联到Element.renderObject属性上,最后再通过RenderObject来完成布局 ...
- 软件测试之Monkey 初步了解(入门级)
monkey 介绍 Monkey是Google提供的一个用于稳定性与压力测试的命令行工具.可以运行在模拟器或者实际设备中.它向系统发送伪随机的用户事件(如按键.手势.触摸屏等输入),对软件进行稳定性与 ...
- Python回归分析五部曲(一)—简单线性回归
回归最初是遗传学中的一个名词,是由英国生物学家兼统计学家高尔顿首先提出来的,他在研究人类身高的时候发现:高个子回归人类的平均身高,而矮个子则从另一方向回归人类的平均身高: 回归分析整体逻辑 回归分析( ...
- kubernetes --- Glusterfs
gluster配额管理gluster volume quota cloud enablegluster volume quota cloud limit-usage /mail/pbs 20MBdd ...
- Redis内存数据库
remote dictionary server 远程字典服务器 Redis默认支持16个数据库,不同的应用应该使用不同的Redis实例存储数据. 支持数据类型:字符串,哈希散列,列表,集合,有序 ...
- CSS Blur() 将高斯模糊应用于输出图片
一.Css Blur() blur() CSS 方法将高斯模糊应用于输出图片. 结果为 <filter-function>. blur(radius) radius模糊的半径,值为< ...
- git rebase 多分支操作
- git rebase and git merge 区别 这一次彻底搞懂 Git Rebase - git在工作中正确的使用方式----git rebase篇 Git 操作假设Git目前只有一个分支 ...
- Salt Highstate数据结构定义
作者言 这篇文档详细解释了SLS文件中,每个部分的名称与含义,以及SLS中的数据处理后的数据结构. 我只是SaltStack的初学者,如果文中有错误的地方,请不吝赐教.在学习的过程,我做了一些实验,犯 ...
- 启用IIS Express SSL(Https)的注意事项
2年前搞国外的信用卡支付对接,必须用SSL方式调用第三方支付公司的接口,本地调试需要启用IIS Express的SSl,最近又搞类似需要SSL的项目,忘记怎么设置的了,本以为直接将原来的http后面加 ...
- Spring Boot Controller单元测试
一.创建Controller 一个方法是用传统IO来下载文件,一个是NIO下载文件 @Controller public class FileController { private Logger l ...