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 ...
随机推荐
- day63-webservice 04.JaxWsServerFactoryBean和SOAP1.2
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap12="h ...
- Oracle 11g RAC for LINUX rhel 6.X silent install(静默安装)
一.前期规划 1.硬件环境 CPU: Intel(R) Xeon(R) CPU E7-4820 v4 @ 2.00GHz 8*10核 内存:512GB OCR:2147*5 MB DATA1:2TB ...
- BZOJ 2140 Tarjan
思路: 跟POJ有一道时限挺长的题一模一样 哦 POJ 1904 题解可以看这个(捂脸) http://blog.csdn.net/qq_31785871/article/details/52963 ...
- jqGrid 排序
jqgrid 排序: 1.前台和后台交互依靠的是index属性,index属性没有设置情况下获取name属性 2.如下状态是经过处理显示的中文,name属性为StatusStr,没有index属性的情 ...
- the selection cannot be run on any server
导入war包后运行jsp 显示: the selection cannot be run on any server 问题原因: Dynamic Web Module 的版本与server不匹配.Dy ...
- 时间&物质&效率
由于我的家庭是地道的农民家庭,在上学的时候,父母很辛苦的供我读初中,高中,大学. 现在我想说的是,用时间来换取效率是我求学时最大的遗憾. 举一个例子吧:每次回家坐火车,火车很费时间,假如我不缺钱,完全 ...
- 工厂模式-CaffeNet训练
参考链接:http://blog.csdn.net/lingerlanlan/article/details/32329761 RNN神经网络:http://nbviewer.ipython.org/ ...
- springmvc中freemarker的搭建
在springmvc框架已经搭好的前提下,导入freemarker.jar,此处版本是2.3. 主要的配置工作都在spring-servlet.xml中,请看代码: <!--配置试图解析器 -- ...
- (转)RabbitMQ学习之消息可靠性及特性
http://blog.csdn.net/zhu_tianwei/article/details/53971296 下面主要从队列.消息发送.消息接收方面了解消息传递过的一些可靠性处理. 1.队列 消 ...
- 用Navicat Prenium12连接Oracle数据库(oracle11g版本)时报错ORA-28547:connection to server failed,probable Oracle Net admin error.解决办法
上网一查原来是oci.dll版本不对.因为Navicat是通过Oracle客户端连接Oracle服务器的,Oracle的客户端分为两种,一种是标准版,一种是简洁版,即Oracle Install Cl ...