package cn.itcast.properties.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 需求:记录程序的运行次数,要是达到5次,就终止程序,并给出必要的提示
*
* 思路:需要一个计数器,这个计数器要记录每次程序的运行次数,并且每次程序启动时都要存在,不能复原。
* 那么久将该数据存入硬盘中,每次程序启动时,限度去这个数据,然后增加一次。
* 读取到的数据存入到集合中,需要键值对,并且余姚IO操作,只能是Properties
* */
public class TestPropertiesDemo {
public static void main(String[] args) throws IOException {
method();
}
public static void method() throws IOException {
//首先把一个文件封装为对象
File configs=new File("conf.properties");
if(!configs.exists()) {
configs.createNewFile();
}
FileInputStream fc=new FileInputStream(configs);
Properties pc=new Properties();
pc.load(fc);
String value=pc.getProperty("count");
int count=0;
if(value!=null)
{
count=Integer.parseInt(value);
if(count>5)
throw new RuntimeException("使用次数已经达到5次,请交钱!");
}
count++;
pc.setProperty("count", Integer.toString(count));
FileOutputStream fcw=new FileOutputStream(configs);
pc.store(fcw, "");
fc.close();
fcw.close();
}
}
JAVA Test_Properities的使用记录程序的运行次数,要是达到5次,就终止程序,并给出必要的提示
猜你喜欢
转载自blog.csdn.net/TDOA1024/article/details/82622955
今日推荐
周排行