- package IO流练习;
- import java.io.*;
- import java.util.*;
- public class 计数器 {
- public static void main(String[] args) throws Exception{
- useCount();
- }
- public static void useCount() throws Exception
- {
- //把目标文件封装成对象
- File file = new File("D:\\pz.in");
- if(!file.exists())
- {
- file.createNewFile();
- }
- //定义一个计数器
- int count = 0;
- Properties prop = new Properties();
- BufferedInputStream bis = new BufferedInputStream(
- new FileInputStream(file));
- prop.load(bis);
- String value = prop.getProperty("time");
- if(value!=null)
- {
- count = Integer.parseInt(value);
- if(count>=3)
- {
- System.out.println("你的使用次数已到,请注册!");
- return ;
- }
- }
- count++;
- prop.setProperty("time",count+"");
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(file));
- prop.store(bos, "comments");
- bis.close();
- bos.close();
-
- }
- }
复制代码
|