IO流实现模拟软件试用的功能

 1 import java.io.*;
 2 
 3 public class TryOut {
 4 
 5     /**
 6      * IO流模拟软件试用次数的功能
 7      * 这里注意try里BufferedOutputStream不要和InputStream放在同一个try里,因为写入的时候他默认会清空原文件的值
 8      *
 9      * @param args
10      */
11     public static void main(String[] args) {
12         try (
13                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("config.txt"))
14         ) {
15             int temp = bis.read();
16             int count = temp ^ 66;
17             if (count > 0 && count <= 3) {
18                 count--;
19                 System.out.println("您的试用次数还剩" + count + "次");
20                 BufferedOutputStream bos = null;
21                 try {
22                     bos = new BufferedOutputStream(new FileOutputStream("config.txt"));
23                     bos.write(count ^ 66);
24                     bos.flush();
25                 } catch (FileNotFoundException e) {
26                     e.printStackTrace();
27                 }
28             } else {
29                 System.out.println("您的试用次数已用尽,请购买正版使用!");
30             }
31         } catch (FileNotFoundException e) {
32             e.printStackTrace();
33         } catch (IOException e) {
34             e.printStackTrace();
35         }
36     }
37 
38     /**
39      * 初始试用次数方法
40      */
41     public static void code() {
42         try (
43                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("config.txt"))
44         ) {
45             bos.write(3 ^ 66);
46             bos.flush();
47         } catch (FileNotFoundException e) {
48             e.printStackTrace();
49         } catch (IOException e) {
50             e.printStackTrace();
51         }
52     }
53 }

大致思路就是把试用次数加密写入到一个txt里,然后通过读取这个txt来让count自减,直到count为0的时候也就代表试用次数用尽了.

转载于:https://www.cnblogs.com/xiaowangtongxue/p/10717130.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值