プログラムの実行回数を記録する機能を定義します.5回満足した後、ヒントを与えて、試用回数はすでに着いて、登録してください

1701 ワード

package com.io.test.properties;

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


public class PropertiesDemo {
	public static void main(String[] args) throws IOException{
		/**
		 *   :      ,         。  5  ,    ,      ,   !
			 *   :
			 * 1,     。
			 * 2,                    ,             。
			 * 	count=1,            ,map  ,          ,  IO  。
			 *   +IO = Properties。 
		 */
		boolean b=checkCount();
		 if(b)
			 run();
	}
	/**
	 * 
	 * @return
	 * @throws IOException 
	 */

	public static boolean checkCount() throws IOException {
		boolean isRun=true;
		//1,        File  。           。
		File file=new File("chen\\ifo.properties");
		if(!file.exists()){//        
			file.createNewFile();
		}
		int count=0;//         
		//Properties prop=new Properties();
		Properties prop =new Properties();
		
		//2、       
		FileInputStream fis=new FileInputStream(file);
		//3,            。
				prop.load(fis);
				
				//4,        。
				String value = prop.getProperty("count");
				
				if(value!=null){
					count = Integer.parseInt(value);
					if(count>=5){
						System.out.println("      ,   ,  !");
						isRun = false;
					}
				}
				count++;//          。
				//  count,             。
				prop.setProperty("count", Integer.toString(count));
				
				//               。
				
				FileOutputStream fos = new FileOutputStream(file);
				prop.store(fos, "");
				
				fos.close();
				fis.close();
				return isRun;
	}
	private static void run() {
		System.out.println("      ");
	}
	

}