最新消息: 新版网站上线了!!!

Java System类用法实战案例

本文实例讲述了Java System类用法。分享给大家供大家参考,具体如下:

一 使用System类访问系统属性

1 代码

import java.util.Map;
public class SystemClassDemo
{
  public static void main(String[] args)
  {
    Map<String, String> env = System.getenv();  // 获得系统的环境变量
    for (String name : env.keySet())
    {
      System.out.println(name + "=" + env.get(name));
    }
  }
}

2 运行

......
ALLUSERSPROFILE=C:\ProgramData
PROCESSOR_ARCHITECTURE=AMD64
PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
SystemDrive=C:
USERNAME=cakin
ProgramFiles(x86)=C:\Program Files (x86)
FPS_BROWSER_USER_PROFILE_STRING=Default
PYTHONPATH=.;E:\Python\MySQLControler
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
DriverData=C:\Windows\System32\Drivers\DriverData
......

二 打印当前虚拟机的所有环境变量和值

1 代码

import java.util.Enumeration;
import java.util.Properties;
public class SystemInfo
{
  public static void main(String[] args)
  {
    Properties sp = System.getProperties(); //获得当前虚拟机的环境属性
    Enumeration e = sp.propertyNames(); //获得环境属性中所有的变量
    //循环打印出当前虚拟机的所有环境属性的变量和值
    while (e.hasMoreElements())
    {
      String key = (String) e.nextElement();
      System.out.println(key + " = " + sp.getProperty(key));
    }
  }
}

2 运行

......
user.variant =
os.name = Windows 10
sun.jnu.encoding = GBK
java.library.path = D:\Program\Java\jdk1.8.0_162\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;D:\Program Files (x86)\scala\bin;D:\Program Files\nodejs\;C:\WINDOWS\System32\OpenSSH\;D:\Program Files\Git\cmd;C:\Users\cakin\AppData\Local\Microsoft\WindowsApps;D:\Program\hadoop-2.7.4\bin;D:\Program\Java\jdk1.8.0_162\bin;C:\Program Files\Microsoft VS Code\bin;D:\Program\apache-maven-3.5.3\bin;C:\Users\cakin\AppData\Roaming\npm;D:\Program\gradle-2.14\bin;D:\Programs\Fiddler;D:\Python27;D:\Python27\Scripts;D:\Anaconda3;D:\Anaconda3\Scripts;D:\Anaconda3\Library\bin;E:\Django\ComPerformance\venv\Lib\site-packages\django;d:\Program Files (x86)\Nmap;.
java.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 10.0
......


转载请注明:谷谷点程序 » Java System类用法实战案例