Java中System类和RunTime类的Api

目录

System 类

1)out

2)err

3)in

4)currentTimeMillis()

5)nanoTime()

6)arraycopy(Object 要从里面复制东西的数组, int 要从里面复制东西数组的索引起始位置, Object 获得复制元素的数组, int 获得复制元素数组的起始索引, int 要复制东西的个数)

7)gc()

8)exit(int status)

9)getProperties()

10.案例:统计程序运行时间

1.代码

2.效果

11.案例:获得系统属性信息

1.代码

2.效果

​编辑

Runtime 类

1)getRuntime()

2)exec(String command)

3)exec(String[] cmdarray)

4)exec(String command, String[] envp)

5)exec(String command, String[] envp, File dir)

6)availableProcessors()

7)freeMemory()

8)totalMemory()

9)maxMemory()

10)addShutdownHook(Thread hook)

11)removeShutdownHook(Thread hook)

12.案例:获取JVM信息

1.代码

2.效果 

13.案例:打开记事本

1.代码

2.效果


System 类

System 类主要用于获取系统属性、标准输入输出流以及执行垃圾回收等操作。它包含了许多静态方法和字段。

  1. 1)out

    • 标准输出流,通常连接到控制台。
    System.out.println("Hello, World!");
  2. 2)err

    • 标准错误流,通常用于打印错误信息。
    System.err.println("This is an error message.");
  3. 3)in

    • 标准输入流,通常从控制台读取数据。
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
  4. 4)currentTimeMillis()

    • 返回当前时间的毫秒表示形式。
    long currentTime = System.currentTimeMillis();
  5. 5)nanoTime()

    • 返回最精确的可用系统计时器的当前值,以纳秒为单位。
    long nanoTime = System.nanoTime();
  6. 6)arraycopy(Object 要从里面复制东西的数组, int 要从里面复制东西数组的索引起始位置, Object 获得复制元素的数组, int 获得复制元素数组的起始索引, int 要复制东西的个数)

    • 从一个数组复制指定范围的元素到另一个数组。
    int[] srcArray = {1, 2, 3, 4, 5};
    int[] destArray = new int[5];
    System.arraycopy(srcArray, 0, destArray, 0, 5);
  7. 7)gc()

    • 建议 JVM 进行垃圾回收。
    System.gc();
  8. 8)exit(int status)

    • 终止当前正在运行的 Java 虚拟机。
    System.exit(0); // 正常退出
  9. 9)getProperties()

    • 获取当前系统的所有属性。
    Properties properties = System.getProperties();
    for (String key : properties.stringPropertyNames()) {
        System.out.println(key + ": " + properties.getProperty(key));
    }

10.案例:统计程序运行时间

1.代码

package org.xiji.system;


public class SystemS {
    public static void main(String[] args)
    {

        //起始时间
        long startTime = System.currentTimeMillis();
        System.out.println("开始时间:"+startTime);
        int sum = 0 ;

        for (int i = 0; i < 1000000000; i++) {
            sum += i;
        }

        //结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("结束时间:"+endTime);
        System.out.println("运行时间:"+(endTime-startTime)+"ms");
    }
}

2.效果

11.案例:获得系统属性信息

1.代码

package org.xiji.system;

import java.util.Enumeration;
import java.util.Properties;

public class MyProperties {
    public static void main(String[] args)
    {

        Properties properties = System.getProperties();
        System.out.println("获得key集合");
        Enumeration<Object> keys = properties.keys();
        System.out.println("系统属性Key:");
        while (keys.hasMoreElements())
        {
            String key = (String) keys.nextElement();
            String value = properties.getProperty(key);
            System.out.println(key+" = "+value);
        }

    }
}

2.效果

可以看到获取了很多系统的信息

Runtime 类

Runtime允许应用程序与其运行环境进行交互。每个 Java 应用程序都有一个 Runtime 实例,可以通过 Runtime.getRuntime() 方法获取。

  1. 1)getRuntime()

    • 返回与当前 Java 应用程序相关的 Runtime 对象。
    Runtime runtime = Runtime.getRuntime();
  2. 2)exec(String command)

    • 执行指定的字符串命令。
    try {
        Process process = runtime.exec("notepad.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }
  3. 3)exec(String[] cmdarray)

    • 使用给定的参数数组执行命令。
    try {
        Process process = runtime.exec(new String[]{"cmd", "/c", "dir"});
    } catch (IOException e) {
        e.printStackTrace();
    }
  4. 4)exec(String command, String[] envp)

    • 在指定的环境变量下执行命令。
    try {
        Process process = runtime.exec("notepad.exe", new String[]{"PATH=C:\\Windows\\System32"});
    } catch (IOException e) {
        e.printStackTrace();
    }
  5. 5)exec(String command, String[] envp, File dir)

    • 在指定的工作目录和环境变量下执行命令。
    try {
        Process process = runtime.exec("notepad.exe", null, new File("C:\\"));
    } catch (IOException e) {
        e.printStackTrace();
    }
  6. 6)availableProcessors()

    • 返回可用处理器的数量。
    int processors = runtime.availableProcessors();
    System.out.println("Number of available processors: " + processors);
  7. 7)freeMemory()

    • 返回 JVM 当前可用的内存数量。
    long freeMemory = runtime.freeMemory();
    System.out.println("Free Memory: " + freeMemory);
  8. 8)totalMemory()

    • 返回 JVM 总共可以使用的内存数量。
    long totalMemory = runtime.totalMemory();
    System.out.println("Total Memory: " + totalMemory);
  9. 9)maxMemory()

    • 返回 JVM 可以使用的最大内存量。
    long maxMemory = runtime.maxMemory();
    System.out.println("Max Memory: " + maxMemory);
  10. 10)addShutdownHook(Thread hook)

    • 注册一个新的虚拟机关闭钩子。
    runtime.addShutdownHook(new Thread(() -> {
        System.out.println("Shutting down...");
    }));
  11. 11)removeShutdownHook(Thread hook)

    • 移除之前注册的虚拟机关闭钩子。
    Thread shutdownHook = new Thread(() -> {
        System.out.println("Shutting down...");
    });
    runtime.addShutdownHook(shutdownHook);
    runtime.removeShutdownHook(shutdownHook);

12.案例:获取JVM信息

1.代码

package org.xiji.system;

public class GetJVMInfo {
    public static void main(String[] args)
    {
        Runtime runtime = Runtime.getRuntime();

        //处理器个数
        System.out.println("处理器个数:"+runtime.availableProcessors());

        //JVM总内存
        System.out.println("JVM总内存:"+runtime.totalMemory()/1024/1024+"MB");

        //JVM空闲内存
        System.out.println("JVM空闲内存:"+runtime.freeMemory()/1024/1024+"MB");
        //JVM最大内存
        System.out.println("JVM最大内存:"+runtime.maxMemory()/1024/1024+"MB");



    }
}

2.效果 

13.案例:打开记事本

1.代码

package org.xiji.system;

public class OpenNotePad {
    public static void main(String[] args)
    {
        Runtime r = Runtime.getRuntime();
        try
        {
            r.exec("notepad");
            System.out.println("记事本已经打开");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

2.效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值