Properties类读写.properties配置文件
package com.hzk.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties; public class PropertiesTools { public static void writeProperties(String filePath, String parameterName,
String parameterValue) {
Properties props = new Properties();
try {
File f = new File(filePath); if (f.exists()) { InputStream fis = new FileInputStream(filePath);
props.load(fis);
fis.close(); } else {
System.out.println(filePath);
f.createNewFile();
} OutputStream fos = new FileOutputStream(filePath);
props.setProperty(parameterName, parameterValue); props.store(fos, "");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static Properties readProperties(String filePath) {
Properties props = new Properties();
InputStream is;
try {
is = new FileInputStream(filePath);
props.load(is);
is.close();
return props;
} catch (Exception e1) {
e1.printStackTrace();
return null;
} } /**
* 写之前将编码转为iso-8859-1,.propertise的默认编码
* @param data
* @return
*/
public static String iso2utf8(String data){
String result = "";
try {
result = new String(data.getBytes("iso-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
} /**
* 读数据的时候转码为utf-8。便于读取
* @param data
* @return
*/
public static String utf82iso(String data){
String result = null;
try {
result = new String(data.getBytes("utf-8"), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
} public static void main(String[] args) {
PropertiesTools.writeProperties("d:\\datas.properties", utf82iso("name"), utf82iso("tom"));
PropertiesTools.writeProperties("d:\\datas.properties", utf82iso("好这口"),utf82iso("hzk"));
PropertiesTools.writeProperties("d:\\datas.properties", utf82iso("hk"),utf82iso("户口"));
Properties props = PropertiesTools.readProperties("d:\\datas.properties");
Enumeration en = props.keys();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String keyDecode = iso2utf8(key);
String value =iso2utf8((String) props.getProperty(key));
System.out.println("key:"+keyDecode+" value:"+value);
}
} }
如上面代码所看到的。注意新建的properties文件的默认编码是iso-8859-1,所以想读写中文数据。都要转码,对于中文会显示成一下形式,见datas.properties:
#
#Sat Jun 14 15:38:10 CST 2014
hk=\u00E6\u0088\u00B7\u00E5\u008F\u00A3
name=tom
\u00E5\u00A5\u00BD\u00E8\u00BF\u0099\u00E5\u008F\u00A3=hzk
假设在myeclipse中保存为utf-8形式,再次能够手动输入中文就能够,可是下次一经代码写入再打开又会变为iso-8859-1的乱码,非常是蛋疼,所以要看中文能够通过代码读取转为utf-8,或者只先存为utf-8格式,编辑中文,不要代码写入中文就能够
Properties类读写.properties配置文件的更多相关文章
- 使用java.util.Properties类读写配置文件
J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...
- java.util.Properties类的介绍-配置文件的读写【-Z-】
简介:java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. #打头的是注释行,Properties会忽略注释.允许只有key ...
- Java中Properties类的操作配置文件
知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...
- Properties类操作.properties配置文件方法总结
一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...
- 基于Java Properties类设置本地配置文件
一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...
- 利用Properties类关联相关配置文件
文件目录 代码: package Lianxi;import java.io.FileInputStream;import java.io.FileNotFoundException;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配置文件
原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...
随机推荐
- git pull VS git fetch&merge(good)
从图中可以看到,git fetch和git pull的区别, git fetch 不会自动的将结果merge到本地,只是将远程版本同步到本地版本库,而不会merge到本地副本. git pull 将 ...
- import android.provider.Telephony cannot be resolved
android.provider.Telephony is hidden. http://androidxref.com/4.0.3_r1/xref/frameworks/base/core/java ...
- android笔记6——intent的使用
今天挑出一节专门来说一下使用intent和intentfilter进行通信. 场景:一个Activity启动还有一个Activity. 前面已经讲了Fragment的切换,Fragment顾名思义是基 ...
- AccountManager教程
API阅读 此类提供所述用户接口到集中登记帐户. 用户只需输入一次帐号password后,您将能够访问internet资源. 不同的在线服务用不同的方式来管理用户,所以account manager ...
- RH133读书笔记(1)-Lab 1 Managing Startup
Lab 1 Managing Startup Goal: To familiarize yourself with the startup process System Setup: A system ...
- Mean Shift简介
Mean Shift,我们 翻译“平均漂移”. 其集群,图像平滑. 图像分割和跟踪已广泛应用.因为我现在认为追踪,因此推出Mean Shift该方法用于目标跟踪.从而MeanShift較全面的介绍. ...
- [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 55273 ...
- Directx11学习笔记【三】 第一个D3D11程序
在先前的解决方案中新建一个新的Win32项目FirstD3D11Demo.在写代码之前,我们必须先添加dx11所需要的库.为了链接dx库,右键项目选择属性->vc++目录,在包含目录中添加你所安 ...
- EL与JSTL注意事项汇总
EL使用表达式(5一个 问题) JSTL使用标签(5问题) 什么是EL.它可以用做? EL全名Expression Language在JSP使用页面 格公式${表达式} 样例${requestScop ...
- NYOJ 118 路方案(第二小的跨越)
修路方案 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军率领着很多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N.因为交通不太便利,南将军准备修 ...