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配置文件的更多相关文章

  1. 使用java.util.Properties类读写配置文件

    J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置,虽说也并非难事,相比 java.util.Properties却要做额外的解析工作.而java.util.Proper ...

  2. java.util.Properties类的介绍-配置文件的读写【-Z-】

    简介:java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. #打头的是注释行,Properties会忽略注释.允许只有key ...

  3. Java中Properties类的操作配置文件

    知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...

  4. Properties类操作.properties配置文件方法总结

    一.properties文件 Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,可以用“#”作为注释,jav ...

  5. 基于Java Properties类设置本地配置文件

    一.Java Properties类介绍 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件, ...

  6. 利用Properties类关联相关配置文件

    文件目录 代码: package Lianxi;import java.io.FileInputStream;import java.io.FileNotFoundException;import j ...

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

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

  8. Java 读写Properties配置文件

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

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

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

随机推荐

  1. WPS2012交叉引用提示word比wps这种强烈的更新参考

                WPS2012交叉引用技巧,word比wps这点强更新參考文献 到时生成仅仅有有一条线,好像WPS不能够,word能够,假设谁知道能够补充.^_^ 1.写论文,參考文献的改动非 ...

  2. MySQL Windows ZIP 免费安装和启动设置

    MySQL Windows ZIP免安装版,设置和启动的过程事实上挺麻烦的.以下一步一步介绍使用的过程: 1.下载Windows (x86, 64-bit), ZIP Archive: 2.解压zip ...

  3. 安装sunvirtualbox

    按照网友提供的方法安装sunvirtualbox,老提示:依赖关系不满足: libpython2.6 (>= 2.6) 后来用终端运行 sudo apt-get install virtualb ...

  4. 第4周 页面限制8060 bytes

    原文:第4周 页面限制8060 bytes 恭喜您!在你面前就只剩下几页了,然后你就可以完成第1个月的SQL Server性能调优培训了.今天我将讲下页的一些限制,还有为什么你会喜欢这些限制,同时也会 ...

  5. 【原创】纯OO:从设计到编码写一个FlappyBird (六)

    第五部分请看这里 终于到了最后一个部分了! 这里使用SimpleJudge类来实现Judge接口. 首先是SimpleJudge需要的实例变量: 0.final LinkedList<Pilla ...

  6. style.display table-row与block

    <tr id="js_rowShow" style=" display:none"> </tr> 问题: display:设置成bloc ...

  7. Android自带样式

    Android系统自带样式: android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 android:the ...

  8. 【C语言的日常实践(十四)】constkeyword详细解释

    const是C语言keyword,它定义一个变量不同意变更.使用const在一定程度上,可以提高节目的安全性和可靠性.其他.解const的作用,在看别人的代码时,对理解对方的程序有一定帮助. 1.co ...

  9. C++习题 对象数组输入与输出

    Description 建立一个对象数组,内放n(n<10)个学生的数据(学号.成绩),用指针指向数组首元素,输出第奇数(1,3,5,7)个学生的数据. Input n和n个学生的学号.成绩 O ...

  10. eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge found

    eclipse git 一个错误:the current branch is not configured for pull No value for key branch.xxx.merge fou ...