2005/04/27
###########################################################################
/*
RWPropertyFile.java
读写删除*.properties文件java.lang.Object
java.util.Dictionary
java.util.Hashtable
java.util.Properties
*/public class RWPropertyFile
extends Properties { String pFilename = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "test.properties"; // 构造文件名
Properties p = null;
FileInputStream in = null; // 构造文件的输入流
FileOutputStream out = null; // 构造文件的输出流 public RWPropertyFile() {
} public RWPropertyFile(String strPropertyFilePathName) {
pFilename = strPropertyFilePathName;
} /**
* 新增、修改key值
*
* 说明:
* 如果指的key不存在,则自动在"第一行"加一行,并填充值
* */
public synchronized boolean modiTheKeyValue(String strKen, String strKeyValue) {
try {
p = new Properties();
in = new FileInputStream(pFilename); // 构造文件的输入流
// native2ascii src.properties dest.properties
p.load(in); // 读入属性
in.close();
}
catch (Exception e) {
System.out.println("Error of create input stream");
System.out.println("maybe the file is not exist");
return false;
}
// System.out.println(p.getProperty("file.encoding"));//null
// System.out.println(System.getProperty("file.encoding"));//GBK
//赋值
try{
strKeyValue = strKeyValue==null?"":strKeyValue;
//行A:
strKeyValue =
new String(strKeyValue.getBytes(System.getProperty("file.encoding")),"ISO8859-1");
p.setProperty(strKen, strKeyValue); // set a new value
}catch(Exception e){} try {
out = new FileOutputStream(pFilename);
//行B:
String fileHeader =
new String("中国".getBytes(System.getProperty("file.encoding")),"ISO-8859-1");
//or ISO8859-1
p.store(out,fileHeader); // 设置属性文件的文件头信息
//save效果一样.不加store或save,会写一个新文件
out.flush();
out.close();
return true;
}
catch (Exception e) {
System.out.println("Error of write input stream");
return false;
}
} /**
* 读取一个值
* */
public Object getKeyValue(String key) {
try {
p = new Properties();
in = new FileInputStream(pFilename); // 构造文件的输入流
p.load(in); // 读入属性
in.close();
return p.getProperty(key);
}
catch (Exception e) {
System.out.println("Error of create input stream");
System.out.println("maybe the file is not exist");
return null;
}
} /**
* 删除一个值
* */
public void delKeyValue(String key) {
try {
p = new Properties();
in = new FileInputStream(pFilename); // 构造文件的输入流
p.load(in); // 读入属性
in.close();
}
catch (Exception e) {
System.out.println("Error of create input stream");
System.out.println("maybe the file is not exist");
} p.remove(key); try {
out = new FileOutputStream(pFilename);
p.store(out, "This file is modified by lza"); // 设置属性文件的文件头信息
out.flush();
out.close();
}
catch (Exception e) {
System.out.println("Error of write input stream");
}
} /**
* 清除值
* */
public void delAll() {
try {
p = new Properties();
in = new FileInputStream(pFilename); // 构造文件的输入流
p.load(in); // 读入属性
in.close(); }
catch (Exception e) {
System.out.println("Error of create input stream");
System.out.println("maybe the file is not exist");
} p.clear(); try {
out = new FileOutputStream(pFilename);
out.flush();
out.close();
}
catch (Exception e) {
System.out.println("Error of write input stream");
}
}
................}###########################################################################
/*
VAD2DataManager.java
提取文件branch.properties中的数据
*/
public class VAD2DataManager { public static void main(String[] arr) {
try {
String s = new java.io.File(
".").getCanonicalPath() +
"\\branch.properties";
voep.datamanager.files.RWPropertyFile rp = new voep.datamanager.files.
RWPropertyFile(s);
//更新password的值
rp.modiTheKeyValue("password", "中国中华人民共和国");
String str = String.valueOf(rp.getKeyValue("password"));
//显示password的值
System.out.println( new String(str.getBytes("iso8859-1"),System.getProperty("file.encoding")) );
System.out.println( str );
//结果说明:
//如果行A,不注释,则显示:
中国中华人民共和国
?????????
//如果行A,注释,则显示:
?????????
中国中华人民共和国
}
catch (Exception e) {}
}
}//结果说明:
//行B
如不进行编码转换,则欲写入文件表头中的“中国”,为乱码。###########################################################################
branch.properties文件用Editplus,notepad等打开:
#中国
#Wed Apr 27 09:43:23 CST 2005
branchid=10960086774164878123551
branchname=\u00CD\u00F8\u00D4\u00BA\u00D6\u00B1\u00CA\u00F4\u00D6\u00D0\u00D0\u00C4
password=\u00D6\u00D0\u00B9\u00FA\u00D6\u00D0\u00BB\u00AA\u00C8\u00CB\u00C3\u00F1\u00B9\u00B2\u00BA\u00CD\u00B9\u00FA
logintimes=5
username=\u00BF\u00C9\u00D2\u00D4\u00CE\u00AA\u00BF\u00D5
###########################################################################
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货