JAVA入門深究之System類

概述

JAVA入門深究之sysout中咱們深究了System.out.println,關於System類,可能這個類方法用的最多就是sout以及System.currentTimeMillis(),下面咱們在研究一下這個類其餘相對重要方法。java

botian_002.jpg

經常使用方法

arraycopy 數組拷貝

/**
@param      src      源數組
@param      srcPos   源數組中的起始位置。
@param      dest     目標數組。
@param      destPos  在目標數據中的起始位置。
@param      length   要複製的數組元素的數量。
@exception  IndexOutOfBoundsException  若是複製會致使在數組範圍以外訪問數據。
@exception  ArrayStoreException  類型不匹配是,必須是數組類型
@exception  NullPointerException 
*/
public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

示例:git

int[] arr1 = { 6, 7, 8, 2, 4 };
int[] arr2 = { 3, 4,5, 8, 9 };

System.arraycopy(arr1, 1, arr2, 1, 3);

arr1[3] = 8;

for (int i = 0; i < 5; i++) {
    System.out.print(arr2[i] + " ");
}
//   3 7 8 2 9

gc垃圾回收

用於運行jvm中的垃圾回收器,完成內存中垃圾清除spring

class Demo {
    /**
     * 垃圾回收器準備釋放內存的時候,會先調用finalize()
     */
    @Override
    public void finalize() {
        System.out.println("gc is comming!!!");
    }
}

public class HelloWorld {
    public static void main(String[] args) {
        new Demo();
        System.gc();
    }
}

getProperties()獲取系統屬性

Properties properties = System.getProperties();
for (String key : properties.stringPropertyNames()) {
    System.out.println(key + "=" + properties.getProperty(key));
}
//打印可參考最下方表格

固然能夠經過System.getProperty(key)獲取指定鍵值chrome

String oName = System.getProperty("os.name");
System.out.println(oName);//Windows 10

System.setProperty(key,value) 能夠設置鍵值對docker

System.setProperties(properties) 能夠設置Properties對象windows

System.clearProperty(key) 根據key刪除property數組

getSecurityManager()安全管理器

jvm運行到未知java程序時,爲了防止惡意代碼對系統影響,須要對運行代碼權限進行控制,這樣就要啓用Java安全管理器安全

使用安全管理器,您能夠控制對:springboot

  1. 文件操做
  2. 反射設施
  3. 讀/寫IO
  4. 線程/線程組操做
  5. 套接字操做(監聽,接受等)
  6. 建立您本身的類加載器的能力。

對於每個這樣的事情,SecurityManager中都有一個check *()方法微信

自定義 securityManager

private static class MySecurityManager extends SecurityManager {

    @Override
    public void checkRead(String file) {
        if ("java".contains(file)) {
            throw new AccessControlException("cannot read file:" + file);
        }
        super.checkRead(file);
    }

}

public static void main(String[] args) throws FileNotFoundException {
    //install
    System.setSecurityManager(new MySecurityManager());
    //read
    InputStream in = new FileInputStream(new File("Hello.java"));
    //uninstall
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        System.setSecurityManager(null);
    }
}

System.setout重定向輸出

如下代碼會將打印內容輸出到txt文本中

PrintStream pStream = new PrintStream("F:/hcx.txt");
pStream.println("666666666666");
System.setOut(pStream);
System.out.println("88888888")
//hcx.txt內容
//666666666666
//88888888

小結

System還有不少方法沒有列出來,不過平常使用已足夠了,但願看完有所獲。若有錯誤,請指正,謝謝

botian_001.jpg

參考

System.properties參考值:

描述
java.runtime.name Java(TM) SE Runtime Environment
sun.boot.library.path C:\Program Files\Java\jdk1.8.0_231\jre\bin
java.vm.version 25.231-b11 Java虛擬機實現版本
java.vm.vendor Oracle Corporation Java 虛擬機實現供應商
java.vendor.url http://java.oracle.com/ Java 供應商的URL
path.separator ; 路徑分隔符(在UNIX系統中是「:」)
java.vm.name Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg sun.io
user.country CN 國家
sun.java.launcher SUN_STANDARD
java.vm.specification.name Java Virtual Machine Specification Java虛擬機規範名稱
user.dir D:\chrome download\dubbo-admin-develop\docker-file-springboot 用戶當前工做目錄
java.runtime.version 1.8.0_231-b11 jdk版本
java.awt.graphicsenv sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs C:\Program Files\Java\jdk1.8.0_231\jre\lib\endorsed endorsed目錄
os.arch amd64 操做系統的架構
java.io.tmpdir C:\Users\12640\AppData\Local\Temp\ io操做歷史文件
java.vm.specification.vendor Oracle Corporation
os.name Windows 10 系統版本
sun.jnu.encoding GBK
java.library.path C:\Program Files\Java\jdk1.8.0_231\bin;C:\windows\Sun ........
java.specification.name Java Platform API Specification Java運行時環境規範名稱
java.class.version 52.0 Java類格式版本號
sun.management.compiler HotSpot 64-Bit Tiered Compilers
os.version 10.0
java.awt.printerjob sun.awt.windows.WPrinterJob
file.encoding UTF-8
java.specification.version 1.8
user.name 12640 用戶的帳戶名稱
java.class.path C:\Program Files\Java\jdk1.8.0 ......
java.vm.specification.version 1.8 java虛擬機規範版本
sun.arch.data.model 64
java.home C:\Program Files\Java\jdk1.8.0_231\jre Java安裝目錄
sun.java.command com.example.HelloWorld 12
java.specification.vendor Oracle Corporation
user.language zh 用戶語言
awt.toolkit sun.awt.windows.WToolkit
java.vm.info mixed mode 混合模式執行
java.version 1.8.0_231
java.ext.dirs C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext;C:\windows\Sun\Java\lib\ext
sun.boot.class.path C:\Program Files\Java\jdk1.8.0_231\jre .....
java.vendor Oracle Corporation
java.vendor.url.bug http://bugreport.sun.com/bugr...
sun.cpu.endian little
sun.io.unicode.encoding UnicodeLittle
sun.desktop windows
sun.cpu.isalist amd64

微信公衆號【Java搬磚小夥子】關注一波,更多資源等着你哦
您的支持是我前進路上最大的動力,謝謝!

qrcode_for_wx.jpg

相關文章
相關標籤/搜索