Properties set small applications -- limit the number of times users use the software. properties --

Source: Internet
Author: User

Properties set small applications -- limit the number of times users use the software. properties --

We can note that some paid software can be used for trial for a certain number of times. After the limit is exceeded, you must purchase a genuine version on the official website to continue using it.

Here we will simply simulate the implementation of this effect.

 

*Requirement: Records the number of startup times of the program. When the number of startup times exceeds 3, the user registration information is required and cannot be used again.
*Ideas:
* 1. A counter is required. After the program starts, the counter is automatically increased based on the original one.
*
* 2. The counter is a variable. Once the program is closed, this variable does not exist. Therefore, you need to make the counter persistent.
* I/O technology is used to store it in the hard disk configuration file.


* 3. Read the configuration file every time the program starts. Numbers are stored directly in files, with unclear information.
* In this case, the key-value pair is used for storage, and the Map set + io technology is used for synthesis, that is, the Properties set.
* In this way, read the configuration file content to the Properties set, and determine the number of times the counter is used,
* If it is less than three times, add the counter and write it back to the configuration file.
* Otherwise, user registration is required and the program is closed.

This Program sets a simple graphical interface and prompt information, the number of use is set to three times.

The Code is as follows:

1 public class PropertiesTest extends JFrame {2 private JLabel show; 3 private JLabel notice; 4 public PropertiesTest () {5 super ("da financial software"); 6 this. setLayout (new BorderLayout (); 7 // create a label and align the label vertically in the center of the display area. 8 show = new JLabel ("welcome to the DA Financial Software ", JLabel. CENTER); 9 notice = new JLabel ("", JLabel. CENTER); 10 this. add (show); 11 this. add (notice, BorderLayout. SOUTH); 12 13 this. setBounds (500,200,300,300); 14 // Add the listener, which is Window settings close action 15 this. addWindowListener (new WindowAdapter () {16 @ Override17 public void windowClosing (invalid wevent arg0) {18 System. exit (0); 19} 20}); 21 this. setVisible (true); 22} 23 public static void main (String [] args) throws IOException, InterruptedException {24 new PropertiesTest (). getCounts (); 25} 26 27 public void getCounts () throws IOException, InterruptedException {28 // encapsulate the File into an object 29 File confile = new File ("count. ini"); 30 if (! Confile. exists () {// the file does not exist 31 confile. createNewFile (); // create a new file 32} 33 FileReader fr = new FileReader (confile); // input stream 34 Properties prop = new Properties (); 35 prop. load (fr); // load file content 36 int count = 0; // used to record program initiation times 37 String value = prop. getProperty ("times"); // get the number of times the object is stored 38 39 if (value! = Null) {// if not started for the first time, 40 count = Integer. parseInt (value); // read the number of times started in the file, so that the number of times is automatically increased by 41 if (count + 1> 3) {// determine the number of times of use 42 // change the tag prompt information 43 show. setText ("the trial is over. Please register it on the official website and try again. Thank you! "); 44 Thread. sleep (3000); 45 System. exit (0); 46} 47} 48 count ++; // whether or not the software is started for the first time, count is increased by 49 notice. setText ("You have used" + count + "Times," + "remaining times" + (3-count) + "times. "); 50 51 // modify the data in the Set 52 prop. setProperty ("times", "" + count); 53 // write the set back to the configuration file 54 FileWriter fw = new FileWriter (confile); 55 prop. store (fw, "Times"); 56 fr. close (); 57 fw. close (); 58} 59}

The first and fourth running interfaces are shown as follows:

The configuration file is used to record the number of times the software is used. More often, the configuration file is used to record the user's personalized settings for the software.

You can customize the software on the graphical interface of the software,

These will be written to the persistent storage in the configuration file of the software, so that the software can read the settings in the configuration file at the next startup.

As long as the configuration file is retained, you can still restore the previous settings of the software after the software is reinstalled.

In Windows, the configuration file ends with ". ini", and in Java, the configuration file ends with ". properties.

XML files are required to store more complex configuration information conveniently.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.