Simple use of the Java configuration information class Properties

Source: Internet
Author: User

Properties: (Configuration information Class) is a collection that represents persistence, inherits Hashtable, and the stored value is a key-worthwhile way
Mainly used for production configuration files and read profile information.

A simple example:

1 Importjava.io.FileNotFoundException;2 ImportJava.io.FileReader;3 ImportJava.io.FileWriter;4 Importjava.io.IOException;5 ImportJava.util.Map.Entry;6 Importjava.util.Properties;7 ImportJava.util.Set;8 9  Public classProperties {Ten  One      Public Static voidMain (string[] args)throwsIOException { A Createpropert (); - Readpropert (); -     } the      -     //(i) Create the configuration information class -      Public Static voidCreatepropert ()throwsIOException { -         //1. Create an Object +Properties pt =NewProperties (); -          +         //2. Configure object information (key values are string types) APt.setproperty ("The Junior", "1101"); atPt.setproperty ("Small Two", "1102"); -Pt.setproperty ("Zhang 3", "1103"); -Pt.setproperty ("Lisi", "1104"); -          -         //3. Save the configured object files to disk (two methods are OK) -         //(1) Store (new FileWriter ("c:\\.")) A character stream is recommended if you need to write to Chinese in         //(2) Store (new FileOutputStream (c:\\.)) BYTE Stream -Pt.store (NewFileWriter ("c:\\users\\bigerf\\desktop\\ Configuration Flow. Properties"), "This is a descriptive message for the file:"); to     } +      -      the     //(ii) Read the configuration object information *      Public Static voidReadpropert ()throwsFileNotFoundException, IOException { $         //1. Create an ObjectPanax NotoginsengProperties pt =NewProperties (); -          the         //2. Read the Configuration object data according to the path load (new FileReader ("c:\\.")) +Pt.load (NewFileReader ("c:\\users\\bigerf\\desktop\\ Configuration flow. Properties")); A          the         //3. Traversing the collection (Configuration object data) +         //A collection is a loop that can be traversed through a foreach -Set<entry<object, object>> Entrys =Pt.entryset (); $          for(Entry<object, object>Entry:entrys) { $System.out.println ("Name:" +entry.getkey () + "ID:" +Entry.getvalue ()); -         } -     } the}

Printing results:

Name:    id:1101name:lisi    ID:1104Name: Small two    ID:1102Name: 3    ID:1103

Related methods:

Construction method: Properties ();//No default value

Properties (properties defaults);//Specify Default values

Configuration information: SetProperties (Key,value);//Key values are string types

Write Data:(1) Store (new FileWriter ("c:\\."), "description of configuration Information"); A character stream is recommended if you need to write to Chinese

(2) Store (new FileOutputStream (c:\\.)  , "Description of the configuration Information"); BYTE stream

Read data: Load (new FileReader ("c:\\.")); Character Stream read

Simple use of the Java configuration information class Properties

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.