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 ...
随机推荐
- 设置web应用的 context root (转)-- web project setting
context root确定了Tomcat将那些URL请求转发到Web应用. 如果应用的context root 设置为 myapp,除非存在更为明确context root的Web应用,那么所有 / ...
- Spring Data JPA:关联映射操作
1.一对一的关系关联 需求:用户和角色一对一关联 package com.example.jpa.pojo; import javax.persistence.*; @Entity @Table(na ...
- JavaScript语法-流程控制语句
一.JavaScript特殊语法 JS特殊语法: 1. 语句以;结尾,如果一行只有一条语句则 ;可以省略 (不建议) 2. 变量的定义使用var关键字,也可以不使用 * 用: 定义的变量是局部变量 * ...
- nginx.conf 配置解析之 全局配置
user nobody; 定义运行nginx服务的用户,还可以加上组,如 user nobody nobody; worker_processes 1; 定义nginx子进程数量,即提供服务的进程数量 ...
- -bash: rvictls: command not found
下载安装Command Line Tools for Xcodehttps://developer.apple.com/download/more/?name=for%20Xcode%20-# 显示包 ...
- 修改git 的远程URL
git remote set-url origin ssh://git@gitlab.tian-wang.com:8022/test/api-automation.git
- data.table
data.table: Extension of 'data.frame' 安装 data.table install.packages("data.table") 官网:http ...
- 有关于服务端模板注入(ssti攻击)——BUUCTF - easy_tornado
打开题目出现3个链接 /flag.txt 中提示flag in /fllllllllllllag /welcome.txt 中提示 render /hints.txt 中提示 md5(cookie_s ...
- ubuntu下Vim安装失败
sudo apt-get install vim Reading package lists... Done Building dependency tree Reading state inform ...
- Fiddler添加过滤条件