1. I have a text file (user.txt) and I know that the data is in the form of a key-value pair, but I don't know what the content is .
Please write a program to determine if there is a "Lisi" such a key exists , if any, change its value to " "
Analysis:
A: Load the data in the file into the collection
B : iterate through the collection to get each key
C : Determine if the key is "Lisi", if any, modify its value to "100"
D : re-storing the data in the collection into a file
2. Code implementation:
1 Packagecn.itcast_08;2 3 ImportJava.io.FileReader;4 ImportJava.io.FileWriter;5 Importjava.io.IOException;6 ImportJava.io.Reader;7 ImportJava.io.Writer;8 Importjava.util.Properties;9 ImportJava.util.Set;Ten One /* A * I have a text file (user.txt) and I know that the data is in the form of a key-value pair, but I don't know what the content is. - * Please write a program to determine if there is a "Lisi" such a key exists, if there is a change in fact, " " - * the * Analysis: - * A: Load the data in the file into the collection - * B: Iterate through the collection to get each key - * C: Determine if the key is "Lisi", if any, modify its value to " + * D: Re-store the data in the collection into a file - */ + Public classPropertiestest { A Public Static voidMain (string[] args)throwsIOException { at //loading data from a file into a collection -Properties prop =NewProperties (); -Reader r =NewFileReader ("User.txt"); - Prop.load (r); - r.close (); - in //iterate through the collection to get each key -Set<string> set =prop.stringpropertynames (); to for(String key:set) { + //determine if the key is "Lisi", if any, modify its value to " - if("Lisi". Equals (Key) { theProp.setproperty (Key, "100"); * Break; $ }Panax Notoginseng } - the //To re-store the data in the collection into a file +Writer W =NewFileWriter ("User.txt"); AProp.store (W,NULL); the w.close (); + } -}
Run the effect as follows:
Java basic Knowledge Hardening IO Flow Note 69:properties exercises to determine if there is a specified key in the file, if there is a case for modifying the value