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

Source: Internet
Author: User

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

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.