System类
system(系统):之前我们使用的输出语句就使用了System类,System类定义了一些与系统相关的属性和方法,它提供的属性和方法均是静态的。因此可以直接调用System类。
System类的常用方法可以通过查询API文档自行查看
1.getProperties()方法
Properties():属性
System类的getProperties()方法用于获取当前系统的全部属性,方法返回一个Properties对象,其中封装了系统的所有属性,属性以键值对形式存在。
例如:
package com.debugtext;
import java.util.Properties;
import java.util.Set;
public class GetPropertiesDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//获取当前系统属性
Properties properties = System.getProperties();
System.out.println(properties);
//获取所有系统属性的key(属性名),返回set对象
Set<String> propertyName = properties.stringPropertyNames();
for(String key : propertyName){
//获取当前键key(属性名)所对应的值
String value =System.getProperty(key);
System.out.println(key+"--->"+value);
}
}
}
user.language—>zh
awt.toolkit—>sun.awt.windows.WToolkit
java.vm.info—>mixed mode
java.version—>1.6.0_13
java.ext.dirs—>D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path—>D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\resources.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\sunrsasign.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\jsse.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\jce.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\charsets.jar;D:\Java\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\classes
java.vendor—>Sun Microsystems Inc.
file.separator—>
java.vendor.url.bug—>http://java.sun.com/cgi-bin/bugreport.cgi
sun.cpu.endian—>little
sun.io.unicode.encoding—>UnicodeLittle
sun.desktop—>windows
sun.cpu.isalist—>pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
2.currentTimeMillis()
currentTimeMillis:电流时间毫秒
currentTimeMillis()方法返回一个long类型的值,该值表示当前时间与1970年1月1日0点0分0秒之间的时间差,单位是毫秒,通常将该值称为时间戳。
现通过一个案例来计算程序在进行求和操作时所消耗的时间。
package com.debugtext;
public class CurrentTimeMillisDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis();
int sum = 0;
for (int i = 0 ; i<100000000; i++ ){
sum+=i;
}
System.out.println(sum);
long endTime = System.currentTimeMillis();
System.out.println("程序运行的时间(单位毫秒)为:"+(endTime-startTime)+"毫秒");
}
}
887459712
程序运行的时间(单位毫秒)为:27毫秒
Tips(小贴士):最大整数加1后会等于最小整数。所以在输出时,会出现输出值为负数的情况。
通过对两个时间戳之间的差值运算,我们可以得到求和操作耗费的时间。
3.arraycopy(Object src , int srcPos , Object dest ,int destPos ,int length)
src:源数组
srcPos:表示源数组中拷贝元素的起始位置。
dest:目标数组
destPos:表示拷贝到目标数组的起始位置
length:数组个数
下面通过一个案例来展示数组的拷贝(注意:进行数组复制时,目标数组必须有足够的空间来存放拷贝的元素,否则hi发生角标越界异常。)
package com.debugtext;
public class ArrayCopyDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] fromArray = {101 , 102 , 103 ,104 , 105 , 106};//原数组
int[] toArray = {201 , 202 , 203 , 204 , 205 ,206 , 207};//目标数组
System.arraycopy(fromArray, 2, toArray, 3, 4);
//打印目标数组
for(int i=0 ; i<toArray.length; i++){//遍历拷贝后的数组toArray的元素
System.out.println(i + ":" + toArray[i]);
}
}
}
0:201
1:202
2:203
3:103
4:104
5:105
6:106
status:状态
除了以上案例涉及到的方法,System类还有两个常见的方法,分别时gc()和exit(int status)。
gc()方法用于启动java的垃圾回收器,并且对内存中的垃圾对象进行回收。
exit(int status)方法用来终止当前正在运行的java的虚拟机,其中的status参数用于表示当前发生的异常状态。通常指定为0,表示正常退出,否则表示异常终止。
具体的使用细则,请查询API文档。
Runtimel类
Runtime类用于表示虚拟机运行的状态,它用于封装JVM虚拟机进程。,欸此使用java命令启动虚拟机,都对应一个RunTime实例,并且只有一个实例,因此该类,采用单例模式及逆行设计,对象不可以直接实例化。若想获得Rumtime实例只能通过这种方式:
Runtime run = Runtime.getRuntime();
由于RunTime类封装了虚拟机进程,因此在程序中通常会通过该类的实例对象来获取当前虚拟机的相关信息。
availableProcessors:可用的处理器
数据传输以2进制表示:
1B(byte,字节)= 8 bit
1KiB(Kibibyte,千字节)=1024B= 2^10 B;
1MiB(Mebibyte,兆字节,百万字节,简称“兆”)=1024KB= 2^20 B;
1GiB(Gibibyte,吉字节,十亿字节,又称“千兆”)=1024MB= 2^30 B。
下面通过一个案例使用Runtime类
package com.debugtext;
public class RunTimeDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runtime run = Runtime.getRuntime();
System.out.println("处理器的个数:"+run.availableProcessors());
System.out.println("空闲内存的数量:"+run.freeMemory()/1024/1024+"M");
System.out.println("最大可用内存数:"+run.maxMemory()/1024/1024+"M");
}
}
处理器的个数:8
空闲内存的数量:4M
最大可用内存数:63M
通过Runtime.getRuntime();方法创建一个Runtime实例对象,并调用方法。注意鉴于计算机性能之间的不同,打印出来的结果也不同。
另外内存以字节为单位计算,通过单位换算成M为单位的值。
exec:执行官
Runtime中提供了一个exec()方法,该方法用于执行一个dos命令,从而是实现与在命令行窗口中输入dos命令同样的效果。
package com.debugtext;
import java.io.IOException;
public class ExecDemo {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Runtime ec = Runtime.getRuntime();//创建Runtime实例对象
ec.exec("Notepad.exe");//调用exec方法
}
}
Runtime类的exec()方法返回一个Process对象,表示操作系统的一个进程,通过该对象可以对产生的新进程进行管理。关闭进程只需要调用destroy()方法即可。
实现打开的记事本在秒后自动关闭的功能:
Unhandled exception type IOException:未处理的异常类型IOException
package com.debugtext;
import java.io.IOException;
public class ExecDemo {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
Runtime ec = Runtime.getRuntime();//创建Runtime实例对象
Process process = ec.exec("Notepad.exe");//调用exec方法得到表示进程的对象
Thread.sleep(3000);//进程休眠3秒
process.destroy();//杀掉进程
}
}
通过调用Process对象的destroy()方法,将打开的记事本关闭。使用Thread类的静态方法sleep(long millis)使程序休眠3秒。因此记事本在打开三秒后关闭。sleep方法可以使程序休眠。