3. Loading and using Properties files; 3. Loading properties

Source: Internet
Author: User

3. Loading and using Properties files; 3. Loading properties
 1. Introduction to Properties

The Properties class is inherited from HashTable and provides similar methods as the implementation class HashMap of Map. It was available in the early stages of Java programming and has almost no changes. The Tiger version of J2SE enhances this class, not only can it be used to specify multiple key-value pairs separated by equal signs in a single row (the key-value pairs and values are separated by equal signs ), you can also load and save these key-value pairs using XML files..

Ii. Loading and simple use of Properties

1. Path Storage

 

2. Information Configuration

 

name = Lilyage   = 22

 

 

 

3. Simple loading and use

 

package com.my.utils;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class LoadProp {        public static void main(String[] args) {        // TODO Auto-generated method stub        Properties prop = new Properties();        InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties");        try {                        prop.load(in);            System.out.println(prop.get("name"));            prop.setProperty("name", "Lucy");            System.out.println(prop.get("name"));                    } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            try {                in.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

 

 

 

4. Use java. util. Enumeration to get all keys

 

package com.my.utils;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Properties;public class LoadProp {    @SuppressWarnings("unchecked")    public static void main(String[] args) {        // TODO Auto-generated method stub        Properties prop = new Properties();        InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties");        Enumeration<String> en = null;        String key = null;        try {                        prop.load(in);                                en = (Enumeration<String>) prop.propertyNames();            while (en.hasMoreElements()) {                key = (String) en.nextElement();                System.out.println(key);                            }                                } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            try {                in.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

 

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.