发个例子大家自己看哈.
package control;
import java.io.bufferedinputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.enumeration;
import java.util.properties;
public class testmain {
//根据key读取value
public static string readvalue(string filepath,string key) {
properties props = new properties();
try {
inputstream in = new bufferedinputstream (new fileinputstream(filepath));
props.load(in);
string value = props.getproperty (key);
system.out.println(key value);
return value;
} catch (exception e) {
e.printstacktrace();
return null;
}
}
//读取properties的全部信息
public static void readproperties(string filepath) {
properties props = new properties();
try {
inputstream in = new bufferedinputstream (new fileinputstream(filepath));
props.load(in);
enumeration en = props.propertynames();
while (en.hasmoreelements()) {
string key = (string) en.nextelement();
string property = props.getproperty (key);
system.out.println(key property);
}
} catch (exception e) {
e.printstacktrace();
}
}
//写入properties信息
public static void writeproperties(string filepath,string parametername,string parametervalue) {
properties prop = new properties();
try {
inputstream fis = new fileinputstream(filepath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 hashtable 的方法 put。使用 getproperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 hashtable 调用 put 的结果。
outputstream fos = new fileoutputstream(filepath);
prop.setproperty(parametername, parametervalue);
//以适合使用 load 方法加载到 properties 表中的格式,
//将此 properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "update '" parametername "' value");
} catch (ioexception e) {
system.err.println("visit " filepath " for updating " parametername " value error");
}
}
public static void main(string[] args) {
readvalue("info.properties","url");
writeproperties("info.properties","age","21");
readproperties("info.properties" );
system.out.println("ok");
}
}
posted on 2006-08-21 15:35
我心依旧 阅读(62384)
评论(15)