3.Properties檔案的載入和使用,3.properties載入

來源:互聯網
上載者:User

3.Properties檔案的載入和使用,3.properties載入
 一.Properties簡介

Properties 類繼承自HashTable,提供的方法很像Map的實作類別HashMap。它在 Java 編程的早期就有了,並且幾乎沒有什麼變化。J2SE 的 Tiger 版本增強了這個類,不僅可以用它在單獨一行中指定用等號分隔的多個鍵-值對(其中鍵和值是用等號分隔的),還可以用XML 檔案裝載和儲存這些鍵-值對。

二.Properties 的載入和簡單的使用

1.路徑存放

 

2.資訊配置

 

name = Lilyage   = 22

 

 

 

3.簡單的載入和使用

 

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.使用 java.util.Enumeration 擷取所有key

 

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();            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.