h3 { margin-top: 0.46cm; margin-bottom: 0.46cm; line-height: 173%; page-break-inside: avoid; }h3.western { font-family: "DejaVu Serif",serif; font-size: 16pt; }h3.cjk { font-family: "DejaVu Sans"; font-size: 16pt; }h3.ctl { font-family: "DejaVu Sans"; font-size: 16pt; }p { margin-bottom: 0.21cm; }a:link { color: rgb(0, 0, 255); }
Properties 类是Hastable 类的子类
增加了将Hastable 对象中的 key 和值保存到文件从文件中读取关键字和值到 Hashtable 对象中的方法
store ( OutputStream out, String comments) Properties 对象中的内容,每个属性的关键字都必须是String 类型 有点类似于存储注释
load ( InputStream inStream)
从输入流中读取属性列表(键和元素对)。
s etProperty ( String key, String value)
调用 Hashtable 的方法 put 。
下面是完成一个检查程序执行了多少次的代码
public static void main(String[] args) {
Properties settings = new Properties();
try {
// 加载配置文件
settings.load( new FileInputStream( "context.conf" ));
} catch (FileNotFoundException e) {
// 如果加载配置文件失败 就是第一次读取 所有要向文件里面写东西
settings.setProperty( "count" , String. valueOf (0));
} catch (IOException e) {
e.printStackTrace();
}
int c = Integer. parseInt (settings.getProperty( "count" ))+1;
System. out .println( " 已经使用了 " +c+ " 次 " );
settings.setProperty( "count" , new Integer(c).toString());
try {
// 存储注释信息
settings.store( new FileOutputStream ( "context.conf" ), "Program is used : " );
} catch ( FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}