java讀取操作系統環境變量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);
}
}
}
更多java讀取操作系統環境變量相關文章請關注PHP中文網!
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!