kosam

任重道远 毋忘奋斗

导航

使用Properties记录程序运行次数

package com.tgx.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class RunCount2 {
	
	private static FileInputStream fis = null;
	private static FileOutputStream fos = null;

	public static void main(String[] args) throws IOException {
				
		try {
			
			Properties prop = new Properties();//创建Properties对象
			
			File file = new File("count2.ini");//定义配置文件对象
			if(!file.exists()){
				
				file.createNewFile();
			}
			//创建文件流
			fis = new FileInputStream(file);
			
			//将文件里的信息载入到prop
			prop.load(fis);
			
			//获取Properties的键值
			String value = prop.getProperty("time");
			
			int count = 0;
			if(value!=null){
				
				count = Integer.parseInt(value);
				//判断程序是否已经使用了5次
				if(count>=5){
					
					System.out.println("您好!你使用的次数已经到了5次了,请注册...");
					return;//退出程序
				}
			}
			count++;
			
			//
			prop.setProperty("time", count+"");
			//将prop写出到file配置文件。
			fos = new FileOutputStream(file);
			prop.store(fos, "");
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
			//关闭资源
			if(fis!=null){
				
				fis.close();
				fis = null;
			}
			if(fos!=null){
				
				fos.close();
				fis = null;
			}
		}
		
	}
}

posted on 2013-01-07 00:06  kosam  阅读(148)  评论(0编辑  收藏  举报