本帖最后由 唐王潮 于 2014-10-8 12:51 编辑
请先看完代码
- package pro.properties;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Properties;
- import java.util.Set;
- public class propertiesTest {
- public static void main(String[] args) throws IOException {
- /*
- * 需求:定义一个功能,记录程序运行的次数。满足5此后,给出提示,试用次数已到,请注册!
- *
- */
- boolean b = checkCount();
- if(b){
- run();
- }
- }
- private static boolean checkCount() throws IOException {
- boolean isrun = true;
-
- File configfile = new File("tempfile\\newinfo.propreties");
- if(!configfile.exists()){
- configfile.createNewFile();
- }
- int count = 0;
-
- Properties prop = new Properties();
- FileInputStream fis = new FileInputStream(configfile);
- // FileOutputStream fos = new FileOutputStream(configfile);//为什么放到此处 写入配置文件中的count的值总是为1??????
- prop.load(fis);
-
- String value = prop.getProperty("count");
- if(value!=null){
- count = Integer.parseInt(value);
- if(count>=5){
- System.out.println("给钱");
- return false;
- }
- }
- count++;
- prop.setProperty("count", Integer.toString(count));
- FileOutputStream fos = new FileOutputStream(configfile);//放到此处没什么问题,正常
-
- prop.store(fos,"app,time");
-
-
- return isrun;
-
-
- }
- private static void run() {
- System.out.println("程序运行");
- }
-
- }
复制代码 |