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 ...
随机推荐
- ALSA安装编程指南
ALSA全指南 一.什么是ALSA ALSA是Advanced Linux Sound Architecture,高级Linux声音架构的简称,它在Linux操作系统上提供了音频和MIDI(Mu ...
- BZOJ 1150 CTSC2007 数据备份Backup 堆+馋
标题效果:给定一个长度n−1n-1的序列,要求选出kk个不相邻的数使得和最小 费用流显然能跑.并且显然过不去- - 考虑用堆模拟费用流 一个错误的贪心是每次取最小.这样显然过不去例子 我们把[每次取最 ...
- schedule vs scheduleAtFixedRate
最好的方法是在两者之间区分 timer灵感时间设定过去时间T,scheduleAtFixedRate将从T现在所有的任务中运行,schedule而该任务将只运行从现在开始计时. public clas ...
- Codeforces 474 E. Pillars
水太...... E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- HDU 4391 Paint The Wall 段树(水
意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...
- STL源代码剖析(一) - 内存分配
Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...
- gerrit git使用
有关git的參考资料 pro git中文版, 最好的git书籍 http://git-scm.com/book/zh 图解git http://marklodato.github.com/visual ...
- 说说PHP的autoLoad自动加载机制
__autoload的使用方法1: 最经常使用的就是这种方法,根据类名,找出类文件,然后require_one 复制代码 代码如下:function __autoload($class_name) { ...
- STL之容器适配器queue的实现框架
说明:本文仅供学习交流,转载请标明出处,欢迎转载! 上篇文章STL之容器适配器stack的实现框架已经介绍了STL是怎样借助基础容器实现一种经常使用的数据结构stack (栈),本文介绍下第二种STL ...