• Java—System类和Runtime类


    System类

    System类介绍

      System类代表Java程序运行平台,程序不能创建该对象,但是System类提供了直接调用的类方法和类变量。
      System类提供标准输入、标准输出、错误输出的类变量;且提供访问环境变量、系统属性、系统时间等静态方法。

    System类用法

    环境变量和系统属性

    public static void main(String[] args) throws Exception {
      //获取所有的系统环境变量
      Map<String, String> env = System.getenv();
      for (String envName : env.keySet()) {
        System.out.printlin(envName + " : " + env.get(envName));
      }
      //获取指定环境变量
      System.out.printlin(System.getenv("JAVA_HOME"));
      //获取所有的系统属性
      Properties properties = System.getProperties();
      //将系统属性保存到文件中
      properties.store(new FileOutputStream("properties.txt"), "System Properties");
      //获取指定的系统属性
      System.out.println(System.getProperty("os.name"));
    }
    

    系统时间

      System类获取系统当前时间的方法:
    currentTimeMillis():返回一个long型整数,返回当前时间与UTC 1970年1月1日午夜的时间差,以毫秒为单位。
    nanoTime():返回一个long型整数,返回当前时间与UTC 1970年1月1日午夜的时间差,以纳秒为单位。

    hash值

      System类提供返回指定对象精确hash值的方法。
    identityHashCode(Object obj):根据对象的地址计算出hashCode值,若两个对象的identityHashCode值相同,则两个对象相同。

    Runtime类

    Runtime类介绍

      Runtime类代表Java程序的运行环境,每个Java程序有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连,应用程序不可以创建自己的Runtime实例,可通过getRuntime()方法获取与之关联的Runtime对象。
      Runtime类提供load(String filename)loadLibrary(String libname)方法加载文件和动态链接库。

  • 相关阅读:
    【lua实战摸索】在b.lua调用a.lua的函数
    【VScode】使用VScode 来写markdown ① 时序图
    修改虚拟机的ip地址
    PyCharm 导包提示 unresolved reference
    报错:Request failed with status code 413
    安装cuda和cudnn
    delphi 10.2报错:connot resolve unit name Forms
    运行delphi 10.2时出现的错误:Error setting debug exception hook
    已知两点坐标和半径,求圆心
    电脑蓝屏,出现BitLocker 恢复
  • 原文地址:https://www.cnblogs.com/Andya/p/12445280.html
Copyright © 2020-2023  润新知