Java property-gets the number of times an application runs

Source: Internet
Author: User

1  PackageFiledemo;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 Importjava.util.Properties;8 9  Public classPropertyTest {Ten  One     /** A      * @paramargs -      * @throwsIOException -      */ the      Public Static voidMain (string[] args)throwsIOException { -  -  -         /* + * Define the function to get the number of times an application runs, if more than 5 times, give the number of times the use has been registered. You do not want to run the program.  -          *  + * Idea: 1, there should be a counter. Each program start is counted once, and is counted on the number of times it was originally made. 2, the counter is a variable.  A * Suddenly come up with an idea, when the program starts counting, the counter must exist in memory and perform the operation.  at * But when the program is over, the counter disappears. Then start the program again and the counter is initialized again. And we need to start the same application multiple times, using the same counter.  - * This requires a longer life cycle of the counter, from memory storage to hard disk files.  -          *  - * 3, how to use this counter? First, when the program starts, it should read the configuration file that is used to log counter information. Gets the last counter count. And make use of the number of judgments.  - * Second, the number of times is self-increment, and the number of times since the increment is re-stored in the configuration file.  -          *  in          *  - * 4, how the information in the file should be stored and reflected. The direct storage value can be, but it is not clear what the data means. So the name becomes very important.  to * This has the corresponding name and value, so you can use a key-value pair. But the mapping relationship map set up, but also need to read the data on the hard disk, so Map+io = + * Properties. -          */ the Getappcount (); *     } $ Panax Notoginseng     Private Static voidGetappcount ()throwsIOException { -File Confile =NewFile ("Count.properties"); the         if(!confile.exists ()) { + confile.createnewfile (); A         } theFileInputStream FIS =NewFileInputStream (confile); +Properties prop =NewProperties (); - prop.load (FIS); $String values = Prop.getproperty ("Time"); $         intCount = 0; -         if(Values! =NULL) { -Count =Integer.parseint (values); the             if(Count > 5) { -                 Throw NewRuntimeException ("Usage limit has been reached");Wuyi             } the         } -count++; WuProp.setproperty ("Time", count+ "" "); -FileOutputStream fos =NewFileOutputStream (confile); AboutProp.store (FOS, ""); $ fos.close (); - fis.close (); -  -     } A}

Java property-gets the number of times an application runs

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.