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 ...
随机推荐
- 编程语言和python介绍, 变量,小整数池,垃圾回收机制
1.编程语言的发展史 计算机是基于电工作(基于高.低电平)1010010101011 1.机器语言 优点:执行速度够快 缺点:开发效率非常低 2.汇编语言(通过英文字符组成) 优点:执行效率相较于机器 ...
- keil中error: #70: incomplete type is not allowed—解决方法
今天在写程序的时候,想使用sizeof求数组的大小,数组中其他c文件定义,在头文件使用extern uint8_t buff_value[]; 声明 但是keil编译报错,网上查了,发现,需要写成ex ...
- python--面向对象编程之学生选课系统练习
1.系统目录结构 文件夹注解: bin--系统管理员和学生的主程序代码 config--系统的配置文件 db--系统的数据文件 admin--管理员的数据文件 student--学生的数据文件 lib ...
- Longest Common Substring SPOJ - LCS (后缀自动机)
Longest Common Substring \[ Time Limit: 294ms \quad Memory Limit: 1572864 kB \] 题意 给出两个串,求两个串的最长公共连续 ...
- MongoDB的安装、基本操作
此说明文档针对的community版本是v4.2.0(1)下载下载官网,此时的community版本是v4.2.0https://www.mongodb.com/download-center/com ...
- 使用Maven创建一个普通java项目
1.创建项目: 使用Maven目的是是我们能够轻松的管理各种别人写过的包 创建好之后,我们去找我们所需要的包:在mvnrepository.com中找自己所需要的包 例子: 最后将依赖写入pom.xm ...
- EF core 性能调优
Entity Framework Core performance tuning – a worked example Last Updated: February 25, 2019 | Create ...
- Odds calculation required for the python strategy library
Bet Class class strats.Bet(inp)[source] Here is an example of the expected string input on instantia ...
- 洛谷2320 bzoj1192 鬼谷子的钱袋
题目链接 题意概述:把正整数n分为m个正整数,m个正整数中不允许出现复数个非1的正整数,保证所有小于n的正整数都可以用一部分正整数的和表示,并且使m尽量小. 这道题不知道为啥bzoj上没有要求输出方案 ...
- nginx 访问控制之 request_uri
$request_uri比$docuemnt_uri多了请求的参数. 主要用来针对请求的uri中的参数进行控制. 示例: if ($request_uri ~ "gid=\d{9,12}&q ...