System
java.lang.System
類定義了系統相關的屬性和操作方法。
成員變量
in 標準輸入流
out 標準輸出流
error 標準錯誤輸出流
成員方法
- 數組拷貝
使用原生方法實現,效率較高。
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
- 返回當前距離格林尼治標準時間 1970年1月1日0時0分0秒的毫秒數
public static long currentTimeMillis()
- 獲取系統屬性
public static Properties getProperties()
public static String getProperty(String key)
// 獲取運行 Java 版本
System.out.println(System.getProperty("java.version"));
// 獲取運行 Java 程序的當前目錄
System.out.println(System.getProperty("user.dir"));
Runtime
Runtime 類主要是關于 Java 應用程序的運行環境,每個 Java 應用程序只有一個 Runtime 實例。
- 獲取運行環境信息
public class Test1 {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("JVM 總內存:" + runtime.totalMemory() + " byte");
}
}
- 調用其他程序
public class Test1 {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
runtime.exec("notepad"); // 打開記事本
}
}