Class Properties
Propertiesclass represents a persisted set of properties . Properties can be saved in a stream or loaded from a stream . each key and its corresponding value in the property list is a string .
Note:properties is a subclass of Hashtable, and the description is a map collection. However, it is not followed by a <>, which means that it is not a generic class
First use it as a map to see:
1Properties prop =NewProperties ();2 3 //adding elements4Prop.put ("it002", "Hello");5Prop.put ("it001", "World");6Prop.put ("it003", "Java");7 8 //System.out.println ("prop:" + prop);9 Ten //iterating through the collection OneSet<object> set =Prop.keyset (); A for(Object key:set) { -Object value =Prop.get (key); -SYSTEM.OUT.PRINTLN (key + "---" +value); the}
Special methods for the properties class:
A, public Object setProperty (String key,string value): add Element
B, public set<string> stringpropertynames (): Gets the collection of all keys
C, public string GetProperty (string key): Gets the element
Example:
1 Public Static voidMain (string[] args) {2 //To create a collection object3Properties prop =NewProperties ();4 5 //Public Object SetProperty (String key,string value): add Element6Prop.setproperty ("Husky", "er ha"));7Prop.setproperty ("Samoyed", "The Little Pizza");8Prop.setproperty ("Garfield", "Fat Man"));9 Ten //Public set<string> stringpropertynames (): Gets the collection of all keys Oneset<string> s =prop.stringpropertynames (); A - //Public string GetProperty (String key): Get element - //Traversal Key Collection the for(String key:s) { -String result =Prop.getproperty (key); -SYSTEM.OUT.PRINTLN (result);//the values are printed out -System.out.println (key+ "----" +result);//a key-value pair is printed out + } -}
combination of Properties collection and IO stream (must be a Properties collection)
A, public void load (Reader reader): reads the data from the file into the collection
B, public void Store (Writer writer,string comments): Storing data in a collection to a file
First try a: publicvoid Load (Reader reader): Read the data in the file into the collection (the data in the file must be in the form of a key-value pair "xxx=yyy")
1 Private Static voidMyload ()throwsIOException {2Properties prop =NewProperties ();3 4 //Public void Load (Reader reader): reads the data from the file into the collection5 //Note: The data for this file must be in the form of a key-value pair6Reader r =NewFileReader ("Prop.txt");7 Prop.load (r);8 r.close ();9 TenSystem.out.println ("Prop:" +prop); One}
The content in Prop.txt is: Husky = two ha
Samoyed = Sam data in the form of key-value pairs
Try B: publicvoid Store (Writer writer,string comments): Store the data in the collection to a file
1 Private Static voidMystore ()throwsIOException {2 //To create a collection object3Properties prop =NewProperties ();4 5Prop.setproperty ("Husky", "er ha"));6Prop.setproperty ("Samoyed", "The Little Pizza");7Prop.setproperty ("Garfield", "Fat Man"));8 9 //Public void Store (Writer writer,string comments): Storing data in a collection to a fileTenWriter W =NewFileWriter ("Name.txt"); OneProp.store (W, "HelloWorld"); A w.close (); -}
In the generated Name.txt file, the contents are also the same as the content format in the Prop.txt, which are key-value pairs
Practical application:
When playing a stand-alone version of the game, the game starts to run, and a game's data folder is automatically generated. And there's a file in it that stores the properties of the game character you're playing.
For example: Play the Three Kingdoms, you will have the generals, the generals will have weapons
Then, when you have this warlord, you click Save, the file will store a key value pair: Bu =1
And when you have a weapon, you'll also store a key-value pair: "Chopper = 1"
。。。 The Properties collection is a persistent collection of attributes
Java 21-14 Properties Class