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