Java中time用什么属性_Java中System类和Runtime类常用方法和属性

本文详细介绍了Java中的System类和Runtime类的主要方法和属性,包括标准输入输出流、系统属性、环境变量的访问、内存管理、垃圾回收以及进程控制等功能。System类提供了获取当前时间、退出JVM、加载库等方法,而Runtime类则涉及运行时环境的管理,如执行外部命令、内存使用情况以及处理器数量等。这两个类在Java应用程序中起到关键作用。
摘要由CSDN通过智能技术生成

1、System类常用方法和属性

System类提供了代表标准輸入、棕准输出和错误输出的类属性;并提供了一些静态方法用于访问环境変量、系统属性的方法;还提供了加载文件和动态链接库的方法。

1)System.err:标准错误输出流。

2)System.in:标准输入流。

3)System.out:标准输出流。

4)System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length):从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。没有返回值。

5)System.clearProperty(String key):根据key移除系统属性。返回系统属性之前的字符串值,如果没有此键的属性,则返回null。

6)System.console():返回与当前 Java 虚拟机关联的唯一 Console 对象,没有返回null。

7)System.currentTimeMillis():返回以毫秒为单位的当前时间。

8)System.exit(int status): 终止当前正在运行的Java虚拟机。status为0是正常退出,非0表示异常退出。

9)System.gc():运行垃圾回收器。

10)System.getenv():返回当前系统环境变量键值映射(Map),并且是不可修改的。

11)System.getenv(String name):获取指定环境变量的值。

12)System.getProperties():获取当前系统属性,返回值是 Properties。

Properties props=System.getProperties(); //系统属性System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));System.out.println("Java的安装路径:"+props.getProperty("java.home"));System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));System.out.println("Java的类路径:"+props.getProperty("java.class.path"));System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));System.out.println("操作系统的名称:"+props.getProperty("os.name"));System.out.println("操作系统的构架:"+props.getProperty("os.arch"));System.out.println("操作系统的版本:"+props.getProperty("os.version")); System.out.println("文件分隔符:"+props.getProperty("file.separator"));//在 unix 系统中是"/"

System.out.println("路径分隔符:"+props.getProperty("path.separator")); //在 unix 系统中是":"System.out.println("行分隔符:"+props.getProperty("line.separator")); //在 unix 系统中是"/n"System.out.println("用户的账户名称:"+props.getProperty("user.name"));System.out.println("用户的主目录:"+props.getProperty("user.home")); System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));

13)System.getProperty(String key) :根据key获取系统属性。和上面的System.getProperties()获取值是一样的,就是调用方法不同。

System.out.println("java版本号:");

System.out.println(System.getProperty("java.version"));

System.out.println("java厂商:");

System.out.println(System.getProperty("java.vendor"));

System.out.println("java厂商网址:");

System.out.println(System.getProperty("java.vendor.url"));

System.out.println("用户名:");

System.out.println(System.getProperty("user.name"));

System.out.println("用户运行程序的当前目录:");

System.out.println(System.getProperty("user.dir"));

System.out.println("用户主目录:");

System.out.println(System.getProperty("uer.home"));

System.out.println("文件分隔符:");

System.out.println(System.getProperty("file.separator"));

System.out.println("操作系统名称:");

System.out.println(System.getProperty("os.name"));

System.out.println("操作系统版本号:");

System.out.println(System.getProperty("os.version"));

14)System.getProperty(String key,String def):key - 系统属性的名称,def - 默认值。返回系统属性的字符串值,如果没有带有此键的属性,则返回默认值。

15)System.getSecurityManager() :获取系统安全接口,返回值是SecurityManager对象。

16)System.identityHashCode(Object x) :返回给定对象的哈希码(int),该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。

17)System.inheritedChannel() :返回从创建此Java虚拟机的实体中继承的信道(Channel)。

18)System.load(String filename):从作为动态库的本地文件系统中以指定的文件名加载代码文件。方法没有返回值。

19)System.loadLibrary(String libname) :加载指定库名称为libname的系统库。方法没有返回值。

20)System.mapLibraryName(String libname):该方法返回一个与平台相关的本地库的名字。返回值为String。

String str = System.mapLibraryName("windows");

System.out.println(str);"输出windows.dll"

21) System.nanoTime(): 返回最准确的可用系统计时器的当前值,以毫微秒为单位。返回值类型为long。

22)System.runFinalization():运行处于挂起终止状态的所有对象的终止方法。方法没有返回值。

23)System.runFinalizersOnExit(boolean value) :已过时。 该方法具有固有的不安全性。它可能对正在使用的对象调用终结方法,而其他线程同时正在操作这些对象,从而导致不正确的行为或死锁。

24)System.setErr(PrintStream err) :重新分配标准错误输出流。

25)System.setIn(InputStream in) : 重新分配“标准”输入流。

26)System.setOut(PrintStream out):重新分配“标准”输出流。

27)System.setProperties(Properties props): 将系统属性设置为 Properties 参数。

28)System.setProperty(String key, String value):设置指定键指示的系统属性。

29)System.setSecurityManager(SecurityManager s):设置系统安全性。

2、Runtime类常用方法和属性

1)Runtime.getRuntime():获取Runtime类实例对象。

2)public Process exec(String command, String[] envp, File dir):用指定的环境和工作目录在指定的进程中执行指定的字符串命令,也可以说就是执行一个另外的程序。

3)public native long freeMemory():该方法返回Java虚拟机中的空闲内存,以字节为单位。

4)public native long totalMemory():该方法用于返回Java虚拟机中的内存总量。

5)public native long maxMemory():该方法用于返回Java虚拟机试图使用的最大内存量。

6)public native int availableProcessors(): 返回Java虚拟机可用的处理器数量。

7)public native void gc():运行垃圾回收器。调用此方法是建议Java虚拟机去回收未使用的对象,以便使它们当前占用的内存能够快速重用。当控件从方法调用返回时,虚拟机已经尽最大努力回收所有丢弃的对象。注意,并不是强制回收,这与System类里面的gc方法是一样的。

8)public void addShutdownHook(Thread hook):shutdown hook就是在JVM关闭的时候执行addShutdownHook添加的钩子,执行完这些钩子(hook),JVM才会关闭。所以这些钩子可以在关闭的时候进行内存清理、对象销毁等操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值