Java中System.setProperty()用法


/*
 * 設置指定鍵對值的系統屬性
 * setProperty (String prop, String value);
 *
 * 參數:
 * prop - 系統屬性的名稱。
 * value - 系統屬性的值。 
 *
 * 返回:
 * 系統屬性以前的值,如果沒有以前的值,則返回 null。
 *
 * 拋出: 
 * SecurityException - 如果安全管理器存在並且其 checkPermission 方法不允許設置指定屬性。
 * NullPointerException - 如果 key 或 value 為 null。
 * IllegalArgumentException - 如果 key 為空。
 * 注:這里的system,系統指的是 JRE (runtime)system,不是指 OS。
 *
*/

//實例
System.setProperty("Property1", "abc");
System.setProperty("Property2","def");

//這樣就把第一個參數設置成為系統的全局變量!可以在項目的任何一個地方 通過System.getProperty("變量");來獲得,

//System.setProperty 相當於一個靜態變量  ,存在內存里面!

public class SystemTest {
    
    static {
        setValue();
    }

    public static void setValue() {
        System.setProperty("name", "張三");
        System.setProperty("age", "28");
    }
    
    public static void main(String[] args) {
        System.out.println(System.getProperty("name"));
        System.out.println(System.getProperty("age"));
    }
}

 輸出:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM