zoukankan      html  css  js  c++  java
  • 使用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;
    			}
    		}
    		
    	}
    }
    

  • 相关阅读:
    前端开发面试题总结之——HTML
    HashMap的那些事
    抽象同步队列AQS(中)—— AQS的重点方法解析
    抽象同步队列AQS(上)—— 宏观上理解AQS
    synchronized原理详解
    volatile原理详解
    JMM模型详解
    计算机运行时内存&处理器CPU初步认知
    mysql-索引详解
    mysql-事务隔离机制&mvcc原理
  • 原文地址:https://www.cnblogs.com/tgxblue/p/4217409.html
Copyright © 2011-2022 走看看