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 配置文件 支持中文 不乱码的更多相关文章

  1. java 顺序 读写 Properties 配置文件

    java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...

  2. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  3. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

  4. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  5. java读写properties配置文件方法

    1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...

  6. (转)Java 读写Properties配置文件

    原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...

  7. Java 读写Properties配置文件【转】

    1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...

  8. 读取 properties 配置文件含有中文的value内容 导致中文乱码 的解决办法

    1.前言 因为装系统的时候把中文写在了系统路径,现在我想把这个路径写在properties里面来读取,可是 发现java 读取会导致中文乱码成 问号????的乱码  ,百度找了好多博客,基本都是一摸一 ...

  9. Springboot 之 解决IDEA读取properties配置文件的中文乱码问题

    问题描述 当在.properties的配置文件中有中文时,读取出来的总是乱码.比如我的application.properties配置文件的内容如下: server.port=9090 test.ms ...

随机推荐

  1. 分布式系统CAP原则与BASE思想

    一.CAP原则 CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者 ...

  2. Object源码分析(一)

    刚注册博客,准备学习一下java源码,当然首先从Object看起. 介绍一下Object: Object是所有类层次结构的根,所有的类都将Object作为超类.所有的对象,包括数组,都实现了Objec ...

  3. [牛客挑战赛 30D] 小A的昆特牌 解题报告 (组合数学)

    interlinkage: https://ac.nowcoder.com/acm/contest/375/D description: solution: 我们枚举步兵的数量$x$,还剩下$S-x$ ...

  4. docker(二):CentOS安装docker

    前置条件 1. CentOS 7:要求系统为64位.系统内核版本为 3.10 以上 使用如下命令,查看机器配置 lsb_release -a uname -a 2. 关闭防火墙 systemctl s ...

  5. selenium 最大化浏览器是解决浏览器和驱动不匹配的方法如下

    那么要想selenium成功的操作chrome浏览器需要经历如下步骤: 1.下载ChromeDriver驱动包(下载地址: http://chromedriver.storage.googleapis ...

  6. javascript变量中基本类型和引用类型的详解解读

    前言: Javascript语言中的变量和其他语言的变量有很大区别,javascript松散类型的本质,决定了它只是在特定时间时间保存特定值得名字而已.由于不存在定义某个变量必须保存何种数据类型值的规 ...

  7. 企业级时间轴插件Vue-timelinepick

    简介 时间范围选择插件 取当前时间之前一段时间范围 按刻,小时,天分类 在线演示及下载 在线演示:https://yelingfeng.github.io/vue-timelinepick/ 本地下载 ...

  8. Spring @Scheduled 注解 定时器例子

    <!--xmlns 多加下面的内容--> xmlns:task="http://www.springframework.org/schema/task" <!-- ...

  9. mysql Seconds_Behind_Master

    通过show slave status查看到的Seconds_Behind_Master,从字面上来看,他是slave落后master的秒数,一般情况下,也确实这样,通过Seconds_Behind_ ...

  10. 【Five-Minute Share】数据库内存分配设置

    对于数据库分配的内存,经常会看到的一条准则是:将服务器75-80%的内存分配给数据库.这是一条比较常见的准则,但并不是绝对的.首先,我们需要保障操作系统运行本身所需的充足内存后,才能去考虑数据库所分配 ...