Java Learning (21): Properties configuration file Read

Source: Internet
Author: User
Tags list of attributes

Properties class inherits from Hashtable
It provides several main methods:
1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. Load (InputStream instream) to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file. To search for GetProperty (String key).
3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.
4. Clear () Clears all loaded key-value pairs. This method is provided in the base class.

Example:

1  Public Static voidMain (string[] args)2     {3         Try4         {5             //get the current project path6Properties props =system.getproperties ();7String ProjectPath = Props.getproperty ("User.dir");8             9             //read the properties fileTenProperties pps =NewProperties (); OneFileInputStream InputStream =NewFileInputStream (ProjectPath + "\\src\\test.properties"); A pps.load (inputstream); -              -             //read value by key theSystem.out.println (Pps.getproperty ("name")); -System.out.println (Pps.getproperty ("Score")); -         } -         Catch(IOException e) +         { -             //TODO auto-generated Catch block + e.printstacktrace (); A         } at}

Java Learning (21): Reading of the properties configuration file

Related Article

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.