java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的。
特从网上查资料,顺序读写的代码,如下,

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set; public class OrderedProperties extends Properties {
private static final long serialVersionUID = -4627607243846121965L;
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 synchronized Object remove(Object key) {
keys.remove(key);
return super.remove(key);
} 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.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties; public class PropertiesTest { public static void main(String[] args) {
String readfile = "D:/eclipseworkspace/test/src/test.txt";
Properties pro = readPropertiesFileObj(readfile); // 读取properties文件
System.out.println(pro.getProperty("password0.9271224287974811"));
pro.remove("password0.008229652622303574");
writePropertiesFileObj(readfile, pro); // 写properties文件
} // 读取资源文件,并处理中文乱码
public static Properties readPropertiesFileObj(String filename) {
Properties properties = new OrderedProperties();
try {
InputStream inputStream = new FileInputStream(filename);
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
properties.load(bf);
inputStream.close(); // 关闭流
} catch (IOException e) {
e.printStackTrace();
}
return properties;
} // 写资源文件,含中文
public static void writePropertiesFileObj(String filename, Properties properties) {
if (properties == null) {
properties = new OrderedProperties();
}
try {
OutputStream outputStream = new FileOutputStream(filename);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
properties.setProperty("username" + Math.random(), "myname");
properties.setProperty("password" + Math.random(), "mypassword");
properties.setProperty("chinese" + Math.random(), "中文");
properties.store(bw, null);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java 顺序 读写 Properties 配置文件的更多相关文章
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- Java加载Properties配置文件工具类
Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...
- java读写properties配置文件方法
1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...
- (转)Java 读写Properties配置文件
原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...
- Java 读写Properties配置文件【转】
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...
- java读写properties配置文件不改变属性的顺序和注释
先贴代码 import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java ...
- Java 读写Properties配置文件(转)
转自:http://www.cnblogs.com/xudong-bupt/p/3758136.html
随机推荐
- UVa 109 - SCUD Busters(凸包计算)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- redis-集群(cluster)扫盲篇(一)
什么是redis的集群 按我个人的理解,redis集群就是实现多个redis节点之间进行数据的共享. 集群有什么好处: 将数据自动split到多个节点进行存储. 当集群中的一部分节点失效或者无法进行通 ...
- android文件存储位置切换
最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占 ...
- 纪录JVM内存模型内容
声明:本内容是博主在牛客网上看到的网友发表的答案,因为感觉总结的比较好,所以摘抄过来供大家学习. 内容: 大多数 JVM 将内存区域划分为 Method Area(Non-Heap)(方法区) ,He ...
- Effective Java 56 Adhere to generally accepted naming conventions
Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...
- SQL Server 2008 R2——开发资料搜集
ADO手册 http://download.csdn.net/detail/wlsgzl/8501115 =============================================== ...
- JPA一对一关联
这里我们仍然是使用annotation对实体进行配置.使用person与idcard模拟一对一的关联关系,一个人只能有一个ID号,同样一个ID号只能对应一个人,人与ID号是一对一的关联关系.Perso ...
- yum命令不能使用的相关错误
使用yum安装软件时,出现下面报错 [root@localhost ~]# yum install -y lrzszLoaded plugins: fastestmirror, securityLoa ...
- Java NIO入门(二):缓冲区内部细节
Java NIO 入门(二)缓冲区内部细节 概述 本文将介绍 NIO 中两个重要的缓冲区组件:状态变量和访问方法 (accessor). 状态变量是前一文中提到的"内部统计机制"的 ...
- C#删除文件和文件夹到回收站
首先对项目添加名为Microsoft.VisualBasic.dll的引用,然后添加命名空间using Microsoft.VisualBasic.FileIO;usingSystem;namespa ...