Java System

 System类代表系统,系统的的属性和很多控制方法都在这个类中,存在于 java.lang中	
由于该类的构造方法为private,所以该类无法实例化(创建对象)。其内部方法都是 static(静态)的,也很方便去调用。

静态成员变量

1.成员

		System.out.println("System.in.toString() = " + System.in.toString());
        System.out.println("System.out.toString() = " + System.out.toString());
        System.out.println("System.err.toString() = " + System.err.toString());

结果:

System.in.toString() = java.io.BufferedInputStream@2e5c649
System.out.toString() = java.io.PrintStream@136432db
System.err.toString() = java.io.PrintStream@7382f612

静态方法

1.获取静态属性–System.getProperty(key)


import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
 
public class TestProperty {
	public static void main(String[] args) {
		System.out.println("Java运行时环境版本:" + System.getProperty("java.version"));
		System.out.println("Java运行时环境供应商:" + System.getProperty("java.vendor"));
		System.out.println("Java供应商的URL: " + System.getProperty("java.vendor.url"));
		System.out.println("Java安装目录: " + System.getProperty("java.home"));
		System.out.println();
		System.out.println("*******************************************************");
		System.out.println("Java虚拟机规范版本:" + System.getProperty("java.vm.specification.version"));
		System.out.println("Java虚拟机规范供应商:" + System.getProperty("java.vm.specification.vendor"));
		System.out.println("Java虚拟机规范名称:" + System.getProperty("java.vm.specification.name"));
		System.out.println("Java虚拟机实现版本:" + System.getProperty("java.vm.version"));
		System.out.println("Java虚拟机实现供应商:" + System.getProperty("java.vm.vendor"));
		System.out.println("Java虚拟机实现名称:" + System.getProperty("java.vm.name"));
		System.out.println();
		System.out.println("*******************************************************");
		System.out.println("Java运行时环境规范版本:" + System.getProperty("java.specification.version"));
		System.out.println("Java运行时环境规范供应商:" + System.getProperty("java.specification.vendor"));
		System.out.println("Java运行时环境规范名称:" + System.getProperty("java.specification.name"));
		System.out.println("Java类格式版本号:" + System.getProperty("java.class.version"));
		System.out.println("Java类路径:" + System.getProperty("java.class.path"));
		System.out.println();
		System.out.println("********************************************************");
		System.out.println("加载库时搜索的路径列表:" + System.getProperty("java.library.path"));
		System.out.println("默认的临时文件路径:" + System.getProperty("java.io.tmpdir"));
		System.out.println("要使用的JIT编译器的名称:" + System.getProperty("java.compiler"));
		System.out.println("一个或多个扩展文件的路径:" + System.getProperty("java.ext.dirs"));
		System.out.println();
		System.out.println("********************************************************");
		System.out.println("操作系统的名称:" + System.getProperty("os.name"));
		System.out.println("操作系统的架构:" + System.getProperty("os.arch"));
		System.out.println("操作系统的版本:" + System.getProperty("os.version"));
		System.out.println("文件分隔符(在UNIX系统中是'/'):" + System.getProperty("file.separator"));
		System.out.println("路径分隔符(在UNIX系统是':'):" + System.getProperty("path.separator"));
		System.out.println("行分隔符(在UNIX系统中是'/n'):" + System.getProperty("line.separator"));
		System.out.println("用户的账户名称:" + System.getProperty("user.name"));
		System.out.println("用户的主目录:" + System.getProperty("user.home"));
		System.out.println("用户的当前工作目录:" + System.getProperty("user.dir"));
		System.out.println();
		
		Properties properties = System.getProperties();
		Set<Entry<Object, Object>> entrySet = properties.entrySet();
		for (Entry<Object, Object> entry : entrySet) {
			System.out.println(entry.getKey() + " = " + entry.getValue());
		}
	}
}

测试结果:

Java运行时环境版本:1.6.0_16
Java运行时环境供应商:Sun Microsystems Inc.
Java供应商的URL: http://java.sun.com/
Java安装目录: D:\esendev\jdk1.6\jre
 
*******************************************************
Java虚拟机规范版本:1.0
Java虚拟机规范供应商:Sun Microsystems Inc.
Java虚拟机规范名称:Java Virtual Machine Specification
Java虚拟机实现版本:14.2-b01
Java虚拟机实现供应商:Sun Microsystems Inc.
Java虚拟机实现名称:Java HotSpot(TM) Client VM
 
*******************************************************
Java运行时环境规范版本:1.6
Java运行时环境规范供应商:Sun Microsystems Inc.
Java运行时环境规范名称:Java Platform API Specification
Java类格式版本号:50.0
Java类路径:D:\esendev\gitrepos\POI\build\classes;D:\esendev\apache-tomcat-5.5\common\lib\commons-el.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-compiler-jdt.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-compiler.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-runtime.jar;D:\esendev\apache-tomcat-5.5\common\lib\jsp-api.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-factory-dbcp.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-factory.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-resources.jar;D:\esendev\apache-tomcat-5.5\common\lib\servlet-api.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-examples-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-excelant-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-ooxml-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-ooxml-schemas-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-scratchpad-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-codec-1.10.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-collections4-4.1.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-logging-1.2.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\junit-4.12.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\log4j-1.2.17.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\ooxml-lib\curvesapi-1.04.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\ooxml-lib\xmlbeans-2.6.0.jar
 
********************************************************
加载库时搜索的路径列表:D:\esendev\jdk1.6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/esendev/jdk1.6/bin/../jre/bin/client;D:/esendev/jdk1.6/bin/../jre/bin;D:/esendev/jdk1.6/bin/../jre/lib/i386;D:\mysql\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\esendev\jdk1.6\bin;D:\esendev\jdk1.6\jre\bin;D:\apache-tomcat-8.0.37\lib;D:\apache-tomcat-8.0.37\bin;D:\esendev\eclipse4git;
默认的临时文件路径:C:\Users\ADMINI~1\AppData\Local\Temp\
要使用的JIT编译器的名称:null
一个或多个扩展文件的路径:D:\esendev\jdk1.6\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
 
********************************************************
操作系统的名称:Windows NT (unknown)
操作系统的架构:x86
操作系统的版本:6.2
文件分隔符(在UNIX系统中是'/'):\
路径分隔符(在UNIX系统是':'):;
行分隔符(在UNIX系统中是'/n'):
 
用户的账户名称:Administrator
用户的主目录:C:\Users\Administrator
用户的当前工作目录:D:\esendev\gitrepos\POI
 
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = D:\esendev\jdk1.6\jre\bin
java.vm.version = 14.2-b01
java.vm.vendor = Sun Microsystems Inc.
java.vendor.url = http://java.sun.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
sun.java.launcher = SUN_STANDARD
user.country = CN
sun.os.patch.level = 
java.vm.specification.name = Java Virtual Machine Specification
user.dir = D:\esendev\gitrepos\POI
java.runtime.version = 1.6.0_16-b01
java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs = D:\esendev\jdk1.6\jre\lib\endorsed
os.arch = x86
java.io.tmpdir = C:\Users\ADMINI~1\AppData\Local\Temp\
line.separator = 
 
java.vm.specification.vendor = Sun Microsystems Inc.
user.variant = 
os.name = Windows NT (unknown)
sun.jnu.encoding = GBK
java.library.path = D:\esendev\jdk1.6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/esendev/jdk1.6/bin/../jre/bin/client;D:/esendev/jdk1.6/bin/../jre/bin;D:/esendev/jdk1.6/bin/../jre/lib/i386;D:\mysql\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\esendev\jdk1.6\bin;D:\esendev\jdk1.6\jre\bin;D:\apache-tomcat-8.0.37\lib;D:\apache-tomcat-8.0.37\bin;D:\esendev\eclipse4git;
java.specification.name = Java Platform API Specification
java.class.version = 50.0
sun.management.compiler = HotSpot Client Compiler
os.version = 6.2
user.home = C:\Users\Administrator
user.timezone = 
java.awt.printerjob = sun.awt.windows.WPrinterJob
file.encoding = UTF-8
java.specification.version = 1.6
java.class.path = D:\esendev\gitrepos\POI\build\classes;D:\esendev\apache-tomcat-5.5\common\lib\commons-el.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-compiler-jdt.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-compiler.jar;D:\esendev\apache-tomcat-5.5\common\lib\jasper-runtime.jar;D:\esendev\apache-tomcat-5.5\common\lib\jsp-api.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-factory-dbcp.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-factory.jar;D:\esendev\apache-tomcat-5.5\common\lib\naming-resources.jar;D:\esendev\apache-tomcat-5.5\common\lib\servlet-api.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-examples-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-excelant-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-ooxml-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-ooxml-schemas-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\poi-scratchpad-3.16.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-codec-1.10.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-collections4-4.1.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\commons-logging-1.2.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\junit-4.12.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\lib\log4j-1.2.17.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\ooxml-lib\curvesapi-1.04.jar;E:\软件\POI\poi-bin-3.16-20170419\poi-3.16\ooxml-lib\xmlbeans-2.6.0.jar
user.name = Administrator
java.vm.specification.version = 1.0
java.home = D:\esendev\jdk1.6\jre
sun.arch.data.model = 32
user.language = zh
java.specification.vendor = Sun Microsystems Inc.
awt.toolkit = sun.awt.windows.WToolkit
java.vm.info = mixed mode
java.version = 1.6.0_16
java.ext.dirs = D:\esendev\jdk1.6\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
sun.boot.class.path = D:\esendev\jdk1.6\jre\lib\resources.jar;D:\esendev\jdk1.6\jre\lib\rt.jar;D:\esendev\jdk1.6\jre\lib\sunrsasign.jar;D:\esendev\jdk1.6\jre\lib\jsse.jar;D:\esendev\jdk1.6\jre\lib\jce.jar;D:\esendev\jdk1.6\jre\lib\charsets.jar;D:\esendev\jdk1.6\jre\classes
java.vendor = Sun Microsystems Inc.
file.separator = \
java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.desktop = windows
sun.cpu.isalist = pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86

数组操作

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
将指定源数组中的数组从指定位置复制到目标数组的指定位置。  

参数:
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。

测试代码

package system;
 
public class TestArraycopy {
 
	public static void main(String[] args) {
		int [] src = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		int [] dest = new int[10];
		System.arraycopy(src, 0, dest, 0, 10);
		System.out.println(pringtArray(dest));
		System.out.println();
		
		int [] dest1 = new int[10];
		System.arraycopy(src, 5, dest1, 0, 5);
		System.out.println(pringtArray(dest1));
		System.out.println();
		
		int [] dest2 = new int[10];
		System.arraycopy(src, 5, dest2, 5, 5);
		System.out.println(pringtArray(dest2));
		System.out.println();
		
//		int [] dest3 = new int[10];
//		System.arraycopy(src, 0, dest3, 0, 11);
//		System.out.println(pringtArray(dest3));
//		System.out.println();
		
		int [] dest4 = new int[10];
		System.arraycopy(src, 0, dest4, 5, 6);
		System.out.println(pringtArray(dest4));
		System.out.println();
	}
 
	private static String pringtArray(int[] dest) {
		StringBuffer sb = new StringBuffer();
		for (int item : dest) {
			sb.append(item).append(", ");
		}
		String str = sb.toString();
		int length = str.length();
		if (length == 0) {
			return null;
		} else {
			return sb.substring(0, length-2);
		}
	}
}

测试结果:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 
5, 6, 7, 8, 9, 0, 0, 0, 0, 0
 
0, 0, 0, 0, 0, 5, 6, 7, 8, 9
 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException  // 越界
	at java.lang.System.arraycopy(Native Method)
	at system.TestArraycopy.main(TestArraycopy.java:28)

获取时间

static long currentTimeMillis() 
返回当前时间(以毫秒为单位)。  当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。

static long nanoTime() 
以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。 取值范围大于约 292 年(263 毫微秒)

测试代码

package system;
 
public class TestTime {
 
	public static void main(String[] args) throws InterruptedException {
		long start = System.currentTimeMillis();
		Thread.sleep(1000);
		System.out.println("耗时:" + (System.currentTimeMillis() - start) + "毫秒");
		
		/**
		 * System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的
		 * 在多核处理器上,由于每个核心的开始时间不确定
		 * 这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑
		 * */
		long startNano = System.nanoTime();
		Thread.sleep(1000);
		System.out.println("耗时:" + (System.nanoTime() - startNano) + "纳秒");
	}
 
}

测试结果:

耗时:1000毫秒
耗时:999973656纳秒
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值