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