Java-System类常用方法详解

这次主要整理下Java中System类的常用方法


一、System类的定义

  • System类为Java的系统类,位于 java.lang 包中
  • System类的构造方法由 private 进行修饰,因此无法被实例化
  • System类提供了标准输入流、标准输出流和错误输出流;
    此外还提供了访问操作系统环境变量,访问虚拟机环境变量,复制数组,垃圾回收等一系列实用方法

二、System类的常用方法

  1、System.currentTimeMillis()

  • 获取当前时间戳,单位为秒

  2、System.nanoTime()

  • 获取当前时间戳,单位为纳秒

  3、System.lineSeparator()

  • 行分隔符,等同于 System.getProperty(“line.separator”)

  4、System.arraycopy()

  • 拷贝数组,有五个参数,System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    • src:源数组
    • srcPos:源数组开始截取的位置,从0开始
    • dest:目标数组
    • destPos:目标数组开始拷贝的位置,从0开始
    • length:截取长度

  5、System.gc()

  • 相当于调用了 Runtime.getRuntime().gc()
    在回收之前会调用对象的finalize()方法,用于负责回收无用对象占据的内存资源
  • 告诉垃圾收集器准备进行垃圾收集,但垃圾收集器不一定会马上执行,
    这是因为 垃圾回收只与内存有关,根据虚拟机的各种算法来计算得到执行垃圾回收的时间

  6、System.runFinalization()

  • 调用已失去引用的对象的finalize()方法,但不能保证其一定执行

  7、System.load(String filepath)

  • 用于加载库文件,参数为 库文件的绝对路径
  • 库文件:动态链接库(ddl),Dynamic Link Library,
    是一个包含可由多个程序同时使用的代码和数据的库,实现程序模块化

  8、System.loadLibrary(String libname)

  • 用于加载库文件,参数为 库文件名
  • 该库文件必须在 java.library.path 所指向的路径中

  9、System.mapLibraryName(String libname)

  • 将库名称映射到特定的字符串中

  10、System.exit(int status)

扫描二维码关注公众号,回复: 2731902 查看本文章
  • 终止目前正在运行的Java虚拟机
  • 参数为0:正常终止
  • 参数非0:异常终止

  11、System.getenv()

  • 获取操作系统的环境变量(即本地系统中的环境变量)
  • 参数为空:获取操作系统的所有环境变量
  • 参数不为空:获取操作系统的指定环境变量(例如:参数为“path”)

  12、System.getProperties()

  • 获取虚拟机(JVM)的所有环境变量

  13、System.getProperty(String key)

  • 获取虚拟机(JVM)的指定环境变量
  • System.getProperty(“java.version”):获取java运行环境版本
  • System.getProperty(“java.vendor”):获取java运行环境供应商
  • System.getProperty(“java.vendor.url”):获取java运行环境供应商的URL
  • System.getProperty(“java.home”):获取java安装路径
  • System.getProperty(“java.vm.specification.version”):获取java虚拟机规范版本
  • System.getProperty(“java.vm.specification.vendor”):获取java虚拟机规范供应商
  • System.getProperty(“java.vm.specification.name”):获取java虚拟机规范名称
  • System.getProperty(“java.vm.version”):获取java虚拟机实现版本
  • System.getProperty(“java.vm.vendor”):获取java虚拟机实现供应商
  • System.getProperty(“java.vm.name”):获取java虚拟机实现名称
  • System.getProperty(“java.specification.version”):获取java运行时环境规范版本
  • System.getProperty(“java.specification.vender”):获取java运行时环境规范供应商
  • System.getProperty(“java.specification.name”):获取java运行时环境规范名称
  • System.getProperty(“java.class.version”):获取java类格式版本号
  • System.getProperty(“jjava.class.path”):获取java类路径
  • System.getProperty(“java.library.path”):获取加载库时搜索的路径列表
  • System.getProperty(“java.io.tmpdir”):获取默认的临时文件路径
  • System.getProperty(“java.ext.dirs”):获取一个或多个扩展目录的路径
  • System.getProperty(“os.name”):获取操作系统的名称
  • System.getProperty(“os.arch”):获取操作系统的构架
  • System.getProperty(“os.version”):获取操作系统的版本
  • System.getProperty(“file.separator”):获取文件分隔符
  • System.getProperty(“path.separator”):获取路径分隔符
  • System.getProperty(“line.separator”):获取行分隔符
  • System.getProperty(“user.name”):获取用户名称
  • System.getProperty(“user.home”):获取用户主目录
  • System.getProperty(“user.dir”):获取用户当前工作目录

  14、System.setProperties(Properties props)

  • 设置虚拟机(JVM)的环境变量(批量)
 Properties prop = new Properties();
 prop.setProperty("aaa", "bbb");
 System.setProperties(prop);
 System.out.println(System.getProperty("aaa"));

  15、System.setProperty(String key, String value)

  • 设置虚拟机(JVM)的环境变量(单个)
System.setProperty("aaa", "bbb");
System.out.println(System.getProperty("aaa"));

  16、System.clearProperty(String key)

  • 清除设置的虚拟机(JVM)的环境变量

  17、System.console()

  • 从控制台设备读取字符信息,只能通过命令执行,在IDE中会报错
Console console = System.console();
System.out.println("please input your name:");
String name = console.readLine();
System.out.println("please input password:");
char[] chars = console.readPassword();
String password = String.valueOf(chars);
System.out.println("your name:" + name + " your password:" + password);
  • 在cmd中先用 javac 命令进行编译,再用 java 命令进行运行
    这里写图片描述

  18、System.setIn(InputStream in)

  • 重新分配标准输入流

  19、System.setErr(PrintStream err)

  • 重新分配标准错误输出流

  20、System.setOut(PrintStream out)

  • 重新分配标准输出流

  21、err

  • 标准错误输出流,没有缓存,会立即输出

  22、out

  • 标准输出流,缓存,不一定会立即输出

  23、System.identityHashCode(Object obj)

  • 根据对象内存地址来计算得到哈希值
  • 注意,这里需要与 hashCode() 方法进行区分
// 由于Apple并没有覆盖hashCode()方法,所以两方法得到的哈希值相等
Apple apple = new Apple();
System.out.println(apple.hashCode()); // 21685669
System.out.println(System.identityHashCode(apple)); // 21685669
// 由于String类覆盖了hashCode()方法,所以两方法得到的哈希值不相等
String str = "123";
System.out.println(str.hashCode()); // 48690
System.out.println(System.identityHashCode(str)); // 21685669
// 由于"123"会存在常量池中,str1和str2两者指向的是常量池中的同一对象,所以两方法得到的哈希值相等
String str1 = "123";
String str2 = "123";
System.out.println(System.identityHashCode(str1)); // 21685669
System.out.println(System.identityHashCode(str2)); // 21685669
// str1和str2是两个不同对象,所以两方法得到的哈希值不相等
String str1 = new String("123");
String str2 = new String("123");
System.out.println(System.identityHashCode(str1)); // 21685669
System.out.println(System.identityHashCode(str2)); // 2133927002

  24、System.setSecurityManager(SecurityManager securityManager)

  • 设置安全管理器,接收一个 SecurityManager 类型的参数

  25、System.getSecurityManager()

  • 获取安全管理器

  26、System.inheritedChannel()

  • 返回从创建此Java虚拟机的实体继承得到的channel

最后三个方法,我到现在为止也没有用过
这里我只是将方法的定义列了出来,至于具体用法,有兴趣的小伙伴可以自行研究一下

猜你喜欢

转载自blog.csdn.net/Goodbye_Youth/article/details/80983007