An example of orderly reading and writing of properties files in Java code
Recently, there was a requirement to read the contents in the properties file for users to modify. After modification, it needs to be saved in the properties file again. Very simple requirements, but the problem is that properties are inherited from hashtable. When traversing the elements in properties directly through keyset(), keys() or entryset() methods, the order of the contents is inconsistent with that in the properties file. This is problem 1; The second problem is that even if it is taken out in order, it is disordered when it is saved to the file.
Of course, there are many ways to solve these two problems. My final approach is to customize a propertiesutil class, which inherits from properties. Propertiesutil provides a method to return a list composed of keys according to the storage order, getkeylist (), so that the problem can be solved as soon as possible. How to ensure that the getkeylist () method returns a collection of ordered keys? I looked at the source of the Properties method, and found that the setproperty () method is actually called the put () method that called the parent class HashTable. Secondly, Properties loads the contents from the file, reads it in file order, and then calls the put () method stored in the parent class HashTable. Therefore, the solution to the problem is that propertiesutil holds a private collection that can store keys in order, then overrides the put () method of the parent class and passes super. In the method body as usual Put() stores the attributes and adds the key to the set storing the key.
Properties provides save () and store () methods, which can store the contents of the current object in the specified output stream, but their underlying logic is the same. Obtain an enumeration by calling the keys () method, then traverse the enumeration, and write the corresponding key and value into the output stream in turn. Therefore, to ensure that the writing is orderly, it is necessary to ensure that the element keys taken out when traversing the enumeration returned by keys () are orderly. Therefore, the solution is to rewrite the keys () method to ensure that the keys obtained when traversing the returned enumeration are orderly.
Here is an example of how to read the properties files in order and write the properties files in the original order.
The above is to inherit Java's own classes. What we do is to achieve order, and the others are just the same as before.
Look at the whole class inheritance relationship as follows:
The following is the class of the main method.
In fact, reading and writing are not different from using the classes provided by the system, but now we have read into the subclasses written by ourselves.
Other code is the same.
The following is a screenshot of the contents of the read file:
Then, the screenshot of the actual code running result:
Well, it's too long. Don't take screenshots. Just show the printed results.
As you can see, only the third time is disordered. I have explained the specific reasons in the code.
Also, a screenshot of the generated file:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.