软件在开始免费使用,在使用次数已到是,将无法运行,并提示客户去注册。
package Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
countApp();
}
public static void countApp() throws IOException {
File file = new File("count.properties");//配置文件
String key = "count";
Properties pro = new Properties();
if(!file.exists()){
file.createNewFile();
}
FileInputStream fis = new FileInputStream(file);
pro.load(fis);
String value = pro.getProperty(key);
int count = 0;
if(value != null){
count = Integer.parseInt(value);
System.out.println("免费试用5次,已试用 "+count+" 次,剩余"+(5-count)+" 次");
if(count >= 5){
throw new RuntimeException("使用次数已到,请注册!");
}
}
count++;
pro.setProperty(key, String.valueOf(count));
FileOutputStream fos = new FileOutputStream(file);
pro.store(fos, key);
fos.close();
fis.close();
}
}