使用Properties 把程序的启动运行次数纪录在某个文件中,每次运行时打印出他的运行次数
import java.util.*;import java.io.*;public class PropertiesFile {/** * @param args */public static void main(String[] args) {Properties pro = new Properties();// 1.创建Properties对象 存储一系列 关键字和对应的值信息try {pro.load(new FileInputStream("count.txt"));// 2.从文件中读取属性 刚开始的关键字和值信息应来自某个文件 所以加载文件} catch (Exception e) {pro.setProperty("count", String.valueOf(0));//如果程序时第一次运行 那么进行初始化 把 count = 0 }//pro.get("count");int c = Integer.parseInt(pro.getProperty("count"))+1;System.out.println("这是第"+ c +"次运行");//3 .装载文件后 打印出运行多少次//pro.put("count", new Integer(c).toString());//put方法可以接收非字符串类型的参数pro.setProperty("count", new Integer(c).toString());//4 .把次数存入到文件中 以便于下一次使用try {pro.store(new FileOutputStream("count.txt"),"program is used num");// 5 .退出的时候把把结果存储到文件当中 把program is used num改成汉语的话 在count.txt 文件当中会出现乱码 这个问题暂时还没有得到解决} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}