打印程序的运行次数
import java.util.*;import java.io.*;
public class PropertiesTest {
/**
* @param args利用properties把程序的启动次数记录在某个文件中,每次运行时打印它的运行次数,可用于计算创建限制
* 运行次数的软件
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Properties setting = new Properties();
try
{
setting.load(new FileInputStream("count.txt"));//从输入流中读取属性列表(键和元素对)。
}catch(Exception e)
{
setting.setProperty("count", String.valueOf(0));
}
int c = Integer.parseInt(setting.getProperty("count"))+1;
System.out.println("这是第"+c+"次运行");
setting.setProperty("count", new Integer(c).toString());//调用 Hashtable 的方法 put。
try
{
setting.store(new FileOutputStream("count.txt"), "propertiestest");//将此 Properties 表中的属性列表(键和元素对)写入输出流。
}catch(Exception e)
{
e.printStackTrace();
}
}
}
测算程序的运行时间代码为:
import java.util.*;
import java.io.*;
public class PropertiesTest {
/**
* @param args利用properties把程序的启动次数记录在某个文件中,每次运行时打印它的运行次数,可用于计算创建限制
* 运行次数的软件
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis();
Properties setting = new Properties();
try
{
setting.load(new FileInputStream("count.txt"));//从输入流中读取属性列表(键和元素对)。
}catch(Exception e)
{
setting.setProperty("count", String.valueOf(0));
}
int c = Integer.parseInt(setting.getProperty("count"))+1;
System.out.println("这是第"+c+"次运行");
setting.setProperty("count", new Integer(c).toString());//调用 Hashtable 的方法 put。
try
{
setting.store(new FileOutputStream("count.txt"), "propertiestest");//将此 Properties 表中的属性列表(键和元素对)写入输出流。
}catch(Exception e)
{
e.printStackTrace();
}
long endtime = System.currentTimeMillis();
System.out.println("the total running time is :" + (endtime - startTime));
}
}
页:
[1]