天天看點

System中的一些屬性(Properties屬性)如何擷取系統屬性和指定系統屬性

import java.util.*;

class SystemDemo

{

public static void main(String[] args) 

{

Properties prop = System.getProperties();

//因為Properties是Hashtable的子類,也就是Map集合的一個子類對象。

//那麼可以通過map的方法取出該集合中的元素。

//該集合中存儲的都是字元串,沒有泛型定義。

//如何在系統中自定義一些特有資訊呢?

System.setProperty("mykey","myvalue");

//擷取指定屬性資訊。

String value = System.getProperty("os.name");

System.out.println("value="+value);

//可不可以在jvm啟動時,動态加載一些屬性資訊呢?

//在CMD控制台中輸入-D<name>=<value>

//java -Dhaha=qqqqq

String v = System.getProperty("haha");

System.out.println("v="+v);

//擷取所有屬性資訊

for(Object obj : prop.keySet())

{

String value = (String)prop.get(obj);

System.out.println(obj+"::"+value);

}

System.out.println("Hello World!");

}

}

繼續閱讀