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 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- 【转】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有特殊的地 ...
- 读取 properties 配置文件含有中文的value内容 导致中文乱码 的解决办法
1.前言 因为装系统的时候把中文写在了系统路径,现在我想把这个路径写在properties里面来读取,可是 发现java 读取会导致中文乱码成 问号????的乱码 ,百度找了好多博客,基本都是一摸一 ...
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
问题描述 当在.properties的配置文件中有中文时,读取出来的总是乱码.比如我的application.properties配置文件的内容如下: server.port=9090 test.ms ...
随机推荐
- 部署微信定位精灵APK到Genymotion
- php auto_load mvc 接口框架(原创)
autoload.php <?php function framework_autoload($className){ $className=str_replace('\\','/',$clas ...
- 解决PopupWindow的阴影覆盖问题
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/108 PopupWindow阴影覆盖问题 最近这段时间需求 ...
- Gerapy 使用详解
https://blog.csdn.net/fengltxx/article/details/79894839
- Dalvik 堆内存管理与回收
Dalvik虚拟机用来分配对象的堆划分为两部分,一部分叫做Active Heap,另一部分叫做Zygote Heap.下面基于管理机制来介绍为何分配为这两部分,以及堆内存的管理. 我们从Android ...
- CSS简单入门
- Java攻城狮学习路线 - 一. 什么是CSS CSS指层叠样式表(Cascading Style Sheets),定义如何显示HTML元素 二. CSS语法 /* 选择器 { 声明: 声明:}* ...
- js-字符串方法
字符串 遍历字符串 方法:(类似数组) 使用for 或 for… in 结果:得到字符串中的每个字符 查找字符 ² charAt(索引值) 注: 超出索引值范围时,则返回空字符 ² ch ...
- <script runat=server>与<%%>,<%=%>与<%response.write%>
我想问一下:在语句<script runat="server"> </script>中编写后台代码和在后台.cs文件中编写后台代码有什么不同,执行效率会不会 ...
- jquery 星级评价插件jquery Raty的使用
需要引入的js <script type="text/javascript" src="<%=basePath%>resources/js/jquery ...
- 【技术累积】【点】【java】【6】时间戳
闲聊 加班多诶,写博客诶. 基本 时间戳,直观理解就是时间上面盖个戳罢了,在时间这个轴上面记录个点: unix时间戳表示从开始的时间点开始,经过了多少秒: 可以简单的看做是一个计时器: 基本定义可以直 ...