Properties 练习
定义功能,获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提升,并不要在运行程序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void main(String[] args) throws IOException { 2 // TODO Auto-generated method stub 3 getAppcount(); 4 } 5 public static void getAppcount() throws IOException{ 6 //将配置文件封装成File对象 7 File confile=new File("count.properties"); 8 if(!confile.exists()){ 9 confile.createNewFile(); 10 } 11 FileInputStream fis=new FileInputStream(confile); 12 13 Properties prop=new Properties(); 14 prop.load(fis); 15 16 //从集合中通过键获取次数 17 String value=prop.getProperty("time"); 18 //定义计数器,记录获取到得次数 19 int count =0; 20 if(value!=null){ 21 count=Integer.parseInt(value); 22 if(count>=5){ 23 throw new RuntimeException("使用次数已到,请注册后使用!!"); 24 } 25 } 26 count++; 27 prop.setProperty("time",count+"" ); 28 FileOutputStream fos=new FileOutputStream(confile); 29 prop.store(fos, "time+count"); 30 31 fis.close(); 32 fos.close(); 33 }