Properties类
Properties类是Hashtable的子类
增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取
关键字和值到Hashtable对象中的方法
如果要用Properties.store方法存储Properties对象中的内容,每个
属性的关键字和值必须是String类型
编程举例:使用Properties把程序的启动运行次数记录在某个文件中,
每次运行时打印出它的运行次数
public static void main(String[] args)
{
Properties settings = new Properties();
try{
settings.load(new FileInputStream("count.txt"));
}
catch(Exception e)
{
settings.setProperty("count",String.valueof(0));
}
int c = Integer.parseInt( settings.getProperty("count") ) + 1;
System.out.println("这是第" + c + "运行");
//settings.put("count",new Integer(c).toString());
settings.setProperty("count",new Integer(c).toString());
try
{
settings.store(new FileOutputStream("count.txt"),"Program is used:");
}
catch(Exception e)
{
e.printStackTrace();
}
}
System和Runtime类
System类
-exit方法结束java虚拟机的运行,异常则传递非0值,如果正常终止则传递0值
-currentTimeMillis方法,获得系统的当前时间
-java虚拟机的系统属性
-getProperties和setProperties方法
Runtime类
-Runtime.getRuntime静态方法
public static void main(String[] args)
{
//System.setProperties();
Properties sp = System.getProperties();
Enumeration e = sp.propertyNames();
while(e.hasMoreElements())
{
String key = (String)e.nextElements();
System.out.println(key + "=" + sp.getProperty());
}
Process p = null; //进程
try
{
p = Runtime.getRuntime().exec("notepad.ext TextProperties.java");
Thread.sleep(5000);
p.destroy();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}