文件名:SysProb.java
描述: 取得當前系統變量的程序。 java中的System.getProperty只是針對JVM來的,如果要取得系統環境變量,還要用到系統相關的函數
作者: 慈勤強
Email :cqq1978@Gmail.com
**/
import java.util.*;
import java.io.*;
class SysProb
{
//返回當前系統變量的函數,結果放在一個Properties里邊,這里只針對win2k以上的,其它系統可以自己改進
public Properties getEnv() throws Exception
{
Properties prop=new Properties();
String OS = System.getProperty("os.name").toLowerCase();
Process p=null;
if(OS.indexOf("windows")>-1)
{
p=Runtime.getRuntime().exec("cmd /c set"); //其它的操作系統可以自行處理, 我這里是win2k
}
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line=br.readLine())!=null)
{
int i=line.indexOf("=");
if(i>-1)
{
String key=line.substring(0,i);
String value=line.substring(i+1);
prop.setProperty(key,value);
}
}
return prop;
}
//具體用法
public static void main(String[] args)
{
try
{
SysProb sp=new SysProb();
Properties p=sp.getEnv();
System.out.println(p.getProperty("Path")); //注意大小寫,如果寫成path就不對了
}
catch(Exception e)
{
System.out.println(e);
}
}
}