System类

来源:互联网 发布:网络语nl什么意思 编辑:程序博客网 时间:2024/05/21 21:38

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}@font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}p{mso-style-noshow:yes;mso-style-priority:99;mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:12.0pt;font-family:宋体;mso-bidi-font-family:宋体;}span.apple-converted-space{mso-style-name:apple-converted-space;mso-style-unhide:no;}span.apple-style-span{mso-style-name:apple-style-span;mso-style-unhide:no;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}-->

         System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。

        由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用。

        1、成员变量

System类内部包含inouterr三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)

                  例如:

                           System.out.println(“Test”);

该行代码的作用是将字符串”Test”输出到系统的标准输出设备上,也就是显示在屏幕上。

后续在学习完IO相关的知识以后,可以使用System类中的成员方法改变标准输入流等对应的设备,例如可以将标准输出流输出的信息输出到文件内部,从而形成日志文件等。

        2、成员方法

                  System类中提供了一些系统级的操作方法,这些方法实现的功能分别如下:

                  aarraycopy方法

publicstatic void arraycopy(Object src, int srcPos, Object dest, int destPos, intlength)

该方法的作用是数组拷贝,也就是将一个数组中的内容复制到另外一个数组中的指定位置,由于该方法是native方法,所以性能上比使用循环高效。

使用示例:

        int[] a = {1,2,3,4};

        int[] b = new int[5];

        System.arraycopy(a,1,b,3,2);

该代码的作用是将数组a中,从下标为1开始,复制到数组b从下标3开始的位置,总共复制2个。也就是将a[1]复制给b[3],将a[2]复制给b[4],这样经过复制以后数组a中的值不发生变化,而数组b中的值将变成{0,0,0,2,3}

                  bcurrentTimeMillis方法

                           public static long currentTimeMillis()

该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)197011000秒所差的毫秒数。例如:

        long l = System. currentTimeMillis();

则获得的将是一个长整型的数字,该数字就是以差值表达的当前时间。

使用该方法获得的时间不够直观,但是却很方便时间的计算。例如,计算程序运行需要的时间则可以使用如下的代码:

        long start = System. currentTimeMillis();

        for(int i = 0;i < 100000000;i++){

                  int a = 0;

        }

        long end = System. currentTimeMillis();

        long  time = end – start;

则这里变量time的值就代表该代码中间的for循环执行需要的毫秒数,使用这种方式可以测试不同算法的程序的执行效率高低,也可以用于后期线程控制时的精确延时实现。

                  cexit方法

                           public static void exit(int status)

该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。

                  dgc方法

                           public static void gc()

该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况。

                  egetProperty方法

                           public static String getProperty(String key)

该方法的作用是获得系统中属性名为key的属性对应的值。系统中常见的属性名以及属性的作用如下表所示。

属性名列表

属性名

属性说明

java.version

Java 运行时环境版本

java.home

Java 安装目录

os.name

操作系统的名称

os.version

操作系统的版本

user.name

用户的账户名称

user.home

用户的主目录

user.dir

用户的当前工作目录

                           例如:

                                    String osName = System.getProperty(“os.name”);

                                    String user = System.getProperty(“user.name”);

                                    System.out.println(“当前操作系统是:”+ osName);

                                    System.out.println(“当前用户是:”+ user);

                          使用该方法可以获得很多系统级的参数以及对应的值。

 

 

 

 

 

 

System.getProperty()参数大全

java.version         Java Runtime Environment version 
java.vendor          Java RuntimeEnvironment vendor 
java.vendor.url     Java vendor URL 
java.home           Java installation directory


java.vm.specification.version        JavaVirtual Machine specification version 
java.vm.specification.vendor        JavaVirtual Machine specification vendor 
java.vm.specification.name         Java Virtual Machine specification name 
java.vm.version                           Java Virtual Machine implementation version 
java.vm.vendor                           Java Virtual Machine implementation vendor 
java.vm.name                             Java Virtual Machine implementation name 
java.specification.version            Java Runtime Environment specification version 
java.specification.vendor            Java Runtime Environment specification vendor 
java.specification.name              Java Runtime Environment specification name 
java.class.version                       Java class format version number 
java.class.path                           Java class path 
java.library.path                          List of paths to search when loading libraries 
java.io.tmpdir                             Default temp file path 
java.compiler                              Name of JIT compiler to use 
java.ext.dirs                                Path of extension directory or directories 
os.name                                      Operating system name 
os.arch                                      Operating system architecture 
os.version                                  Operating system version 
file.separator                             File separator ("/" on UNIX) 
path.separator                           Path separator (":" on UNIX) 
line.separator                              Line separator ("/n" on UNIX) 
user.name                                   User's account name 
user.home                                   User's home directory 
user.dir                                        User's current working directory

 

原创粉丝点击