《疯狂Java讲义》(二十) ---- 系统相关类

  • System类

  代表当前Java程序的运行平台。

  • Runtime类

  代表Java程序的运行时环境,每个Java程序都有一个与之对应的Runtime实例,应用程序不能创建自己的Runtime实例,但可以通过Runtime.getRuntime()方法获取与之相关的Runtime对象。可以访问JVM的相关信息,如处理器数量/内存信息等。

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

public class SystemTest {

    public static void main(String[] args) throws Exception, IOException {
        Map<String, String> env = System.getenv();
        for (String name : env.keySet()) {
            System.out.println(name + "--->" + env.get(name));
        }
        
        System.out.println(System.getenv("JAVA_HOME"));
        Properties props = System.getProperties();
        props.store(new FileOutputStream("props.txt"), "System Properties");
        
        System.out.println(System.getProperty("os.name"));
    }
}
原文地址:https://www.cnblogs.com/IvySue/p/6347187.html