Java System 类
最后修改时间:2025 年 4 月 13 日
java.lang.System
类提供了系统级别的功能,包括标准输入、输出、错误流以及对系统属性的访问。它包含有用的类字段和方法,但不能被实例化。
System 类是 final 的,并且它的所有方法都是静态的,无需创建实例即可访问它们。它提供了用于环境变量、当前时间测量、数组复制和系统属性访问的方法。
System 类方法
System 类提供了几个重要的静态方法,用于与系统环境交互。这些方法包括用于 I/O 操作、属性访问、时间测量和数组操作的方法。该类不能被实例化。
public final class System { public static final PrintStream out; public static final PrintStream err; public static final InputStream in; public static long currentTimeMillis() {...} public static long nanoTime() {...} public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {...} public static String getProperty(String key) {...} public static String getenv(String name) {...} public static void exit(int status) {...} public static void gc() {...} }
上面的代码显示了 System 类的关键字段和方法。这些实用程序是 Java 应用程序中各种系统操作的基础。
标准 I/O 流
System 类提供了三个标准 I/O 流:in
、out
和 err
。这些用于读取输入、写入输出和错误消息。如果需要,它们可以被重定向。
package com.zetcode; import java.util.Scanner; public class Main { public static void main(String[] args) { // Using standard output stream System.out.println("This is standard output"); // Using standard error stream System.err.println("This is standard error"); // Using standard input stream Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name); scanner.close(); } }
此示例演示了三个标准流。System.out
打印到标准输出,System.err
打印到标准错误,而 System.in
使用 Scanner 从标准输入读取。
currentTimeMillis 和 nanoTime
currentTimeMillis
和 nanoTime
方法提供了时间测量功能。currentTimeMillis
返回当前时间(以毫秒为单位),而 nanoTime
提供了纳秒精度,用于测量经过的时间。
package com.zetcode; public class Main { public static void main(String[] args) { // Using currentTimeMillis for wall-clock time long startTime = System.currentTimeMillis(); // Using nanoTime for high-resolution timing long nanoStart = System.nanoTime(); // Perform some operation long sum = 0; for (int i = 0; i < 1000000; i++) { sum += i; } long nanoEnd = System.nanoTime(); long endTime = System.currentTimeMillis(); System.out.println("Sum: " + sum); System.out.println("Elapsed time (ms): " + (endTime - startTime)); System.out.println("Elapsed time (ns): " + (nanoEnd - nanoStart)); } }
此示例显示了这两种计时方法。currentTimeMillis
适用于墙上时钟时间测量,而 nanoTime
提供了更精确的计时,用于性能测量。请注意,nanoTime 值仅对经过的时间计算有意义。
arraycopy 方法
arraycopy
方法提供了一种在数组之间复制数据的有效方法。它是一种 native 方法,通常比使用循环手动复制数组更快。该方法处理源数组和目标数组重叠的情况。
package com.zetcode; public class Main { public static void main(String[] args) { int[] source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[] destination = new int[10]; // Copy entire array System.arraycopy(source, 0, destination, 0, source.length); System.out.println("Full copy: " + java.util.Arrays.toString(destination)); // Copy partial array int[] partialDest = new int[5]; System.arraycopy(source, 2, partialDest, 0, 5); System.out.println("Partial copy: " + java.util.Arrays.toString(partialDest)); // Overlapping copy System.arraycopy(source, 0, source, 3, 4); System.out.println("Overlapping copy: " + java.util.Arrays.toString(source)); } }
此示例演示了 arraycopy
的三种用法:完整数组复制、部分复制和重叠复制。该方法是高效的,并正确处理所有情况,包括源数组和目标数组相同的情况。
getProperty 和 getProperties
getProperty
和 getProperties
方法提供对系统属性的访问。这些属性包括关于 Java 运行时、操作系统、用户配置等信息。属性可以在启动时设置。
package com.zetcode; public class Main { public static void main(String[] args) { // Get single property String javaVersion = System.getProperty("java.version"); String osName = System.getProperty("os.name"); System.out.println("Java version: " + javaVersion); System.out.println("OS name: " + osName); // Get all properties System.out.println("\nAll system properties:"); java.util.Properties props = System.getProperties(); props.list(System.out); // Set and get custom property System.setProperty("custom.property", "example.value"); System.out.println("\nCustom property: " + System.getProperty("custom.property")); } }
此示例演示了如何访问系统属性。我们检索特定属性,列出所有属性,并演示设置自定义属性。系统属性对于配置和环境检测非常有用。
getenv 方法
getenv
方法提供对来自系统的环境变量的访问。这些变量通常在操作系统 shell 中设置,并且对所有进程都可用。该方法可以访问特定变量或所有变量。
package com.zetcode; public class Main { public static void main(String[] args) { // Get specific environment variable String path = System.getenv("PATH"); String home = System.getenv("HOME"); System.out.println("PATH: " + path); System.out.println("HOME: " + home); // Get all environment variables System.out.println("\nAll environment variables:"); java.util.Map<String, String> env = System.getenv(); for (String key : env.keySet()) { System.out.println(key + "=" + env.get(key)); } } }
此示例演示了访问环境变量。我们展示了如何获取特定变量,如 PATH 和 HOME,以及如何列出所有环境变量。环境变量对于系统配置很有用。
exit 和 gc 方法
exit
方法终止当前正在运行的 Java 虚拟机,而 gc
建议 JVM 运行垃圾回收。exit
应该谨慎使用,因为它会停止所有程序线程。
package com.zetcode; public class Main { public static void main(String[] args) { // Demonstrate garbage collection Runtime runtime = Runtime.getRuntime(); System.out.println("Before GC - Free memory: " + runtime.freeMemory()); System.gc(); // Suggest garbage collection System.out.println("After GC - Free memory: " + runtime.freeMemory()); // Demonstrate exit System.out.println("Program running..."); System.exit(0); // Normal termination System.out.println("This won't be printed"); } }
此示例显示了 gc
建议进行垃圾回收(请注意,JVM 可能会忽略此请求),以及 exit
终止程序。退出状态码(此处为 0)通常表示成功(非零表示错误)。
来源
在本文中,我们通过实际示例介绍了 Java System 类的基本方法。这些实用程序是 Java 应用程序中系统交互的基础,从 I/O 操作到环境访问和计时。
作者
列出所有Java教程。