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. Windows Phone开发(17):URI映射

    原文:Windows Phone开发(17):URI映射 前面在讲述导航的知识,也讲了控件,也讲了资源,样式,模板,相信大家对UI部分的内容应该有了很直观的认识了.那么今天讲什么呢?不知道大家在练习导 ...

  2. [Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误

    dele_id = Fee_details_invoices.objects.filter(fee_detail_id__in=fee_id_list, return_type='2').values ...

  3. 深入探索C++对象模型-语义

    有三种情况,这将是一个object的内容,以及一class object早期值: class X { ... }; X x; X xx = x;               // 情况1,赋值对象 e ...

  4. 【Android先进】查看手机记忆库状态和应用方法

    一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...

  5. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  6. Android KitKat 4.4 Wifi移植AP模式和网络共享的调试日志

    Tethering技术在移动平台上已经运用的越来越广泛了.它能够把移动设备当做一个接入点,其它的设备能够通过Wi-Fi.USB或是Bluetooth等方式连接到此移动设备.在Android中能够将Wi ...

  7. T-SQL基础(7) - 透视,逆透视和分组集

    透视转换: use tempdb;if object_id('dbo.Orders', 'U') is not null drop table dbo.Orders;create table dbo. ...

  8. 3、Spring4之Bean 配置的细节

    1). 若字面值中包括特殊字符,则能够使用 value 节点的 <![CDATA[]]> 把字面值包裹起来.      <constructor-arg>           ...

  9. android AlarmManager采用

    Android的闹钟实现机制非常easy, 仅仅须要调用AlarmManager.Set()方法将闹钟设置提交给系统,当闹钟时间到后,系统会依照我们的设定发送指定的广播消息.我们写一个广播去接收消息做 ...

  10. Scala---For语句段

    For语句段 语法: Expr1 ::= „for‟ („(‟ Enumerators „)‟ | „{‟ Enumerators „}‟) {nl} [„yield‟] Expr Enumerato ...