java_IO_properties_一个简易程序运行次数判断器
1 package properties; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.util.Properties; 8 /** 9 * 需求:设计一个程序运行次数的判断器,如果启动五次,给出使用次数已到,请注册的提示。。。。 10 11 思路: 1,要有计数器count,自增,判断。 12 * 2,count要存在配置文件中。 13 * 3,启动程序时,读取配置文件,进行判断,自增后,要写回配置文件。 14 * 4,键值对,map+io = properties。 15 * @author wkai 16 * 17 */ 18 public class PropertiesTest { 19 20 21 public static void main(String[] args) throws IOException { 22 23 getAppCount(); 24 } 25 26 private static void getAppCount() throws IOException { 27 //获取配置文件,没有,则建立 28 File inf = new File("D:\\inf.properties"); 29 if(!inf.exists()){ 30 inf.createNewFile(); 31 } 32 33 FileInputStream fis = new FileInputStream(inf); 34 35 Properties prop = new Properties(); 36 37 prop.load(fis); 38 //prop.list(System.out); 39 40 41 int count = 0; 42 String count_value = prop.getProperty("count"); 43 if(count_value!=null){ 44 count = Integer.parseInt(count_value); 45 System.out.println("您已使用"+count+"次"); 46 if(count>5) 47 throw new RuntimeException("使用次数已到,请注册,给钱!"); 48 } 49 50 count++; 51 prop.setProperty("count",""+count); 52 53 FileOutputStream fos = new FileOutputStream(inf); 54 prop.store(fos, ""); 55 fos.close(); 56 fis.close(); 57 } 58 59 60 }