Java System学习
一.概述
System
类包含一些有用的类字段和方法。它不能被实例化。
在 System
类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。
由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用。
二.初始化
/**
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
props = new Properties();
initProperties(props);
sun.misc.Version.init();
// Workaround until DownloadManager initialization is revisited.
// Make JavaLangAccess available early enough for internal
// Shutdown hooks to be registered
setJavaLangAccess();
// Gets and removes system properties that configure the Integer
// cache used to support the object identity semantics of autoboxing.
// At this time, the size of the cache may be controlled by the
// vm option -XX:AutoBoxCacheMax=<size>.
Integer.getAndRemoveCacheProperties();
// 现在加载zip库,以保持java.util.zip.ZipFile尝试使用自己以后加载该库。
// Load the zip library now in order to keep java.util.zip.ZipFile from trying to use itself to load this library later.
loadLibrary("zip");
// 初始化in、out、err
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
// Setup Java signal handlers for HUP, TERM, and INT (where available).
Terminator.setup();
// Initialize any miscellenous operating system settings that need to be
// set for the class libraries. Currently this is no-op everywhere except
// for Windows where the process-wide error mode is set before the java.io
// classes are used.
sun.misc.VM.initializeOSEnvironment();
// Set the maximum amount of direct memory. This value is controlled
// by the vm option -XX:MaxDirectMemorySize=<size>. This method acts
// as an initializer only if it is called before sun.misc.VM.booted().
sun.misc.VM.maxDirectMemory();
// Set a boolean to determine whether ClassLoader.loadClass accepts
// array syntax. This value is controlled by the system property
// "sun.lang.ClassLoader.allowArraySyntax". This method acts as
// an initializer only if it is called before sun.misc.VM.booted().
sun.misc.VM.allowArraySyntax();
// Subsystems that are invoked during initialization can invoke
// sun.misc.VM.isBooted() in order to avoid doing things that should
// wait until the application class loader has been set up.
sun.misc.VM.booted();
// The main thread is not added to its thread group in the same
// way as other threads; we must do it ourselves here.
Thread current = Thread.currentThread();
current.getThreadGroup().add(current);
}
三.静态成员变量
1.静态成员变量
2.测试代码
package system;
public class TestSystem {
public static void main(String[] args) {
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());
}
}
3.测试结果
四.静态方法
1.获取属性 - System.getProperty(key)
Java.version |
Java 运行时环境版本 |
java.vendor |
Java 运行时环境供应商 |
java.vendor.url |
Java 供应商的 URL |
java.home |
Java 安装目录 |
java.vm.specification.version |
Java 虚拟机规范版本 |
java.vm.specification.vendor |
Java 虚拟机规范供应商 |
java.vm.specification.name |
Java 虚拟机规范名称 |
java.vm.version |
Java 虚拟机实现版本 |
java.vm.vendor |
Java 虚拟机实现供应商 |
java.vm.name |
Java 虚拟机实现名称 |
java.specification.version |
Java 运行时环境规范版本 |
java.specification.vendor |
Java 运行时环境规范供应商 |
java.specification.name |
Java 运行时环境规范名称 |
java.class.version |
Java 类格式版本号 |
java.class.path |
Java 类路径 |
java.library.path |
加载库时搜索的路径列表 |
java.io.tmpdir |
默认的临时文件路径 |
java.compiler |
要使用的 JIT 编译器的名称 |
java.ext.dirs |
一个或多个扩展目录的路径 |
os.name |
操作系统的名称 |
os.arch |
操作系统的架构 |
os.version |
操作系统的版本 |
file.separator |
文件分隔符(在 UNIX 系统中是“/”) |
path.separator |
路径分隔符(在 UNIX 系统中是“:”) |
line.separator |
行分隔符(在 UNIX 系统中是“/n”) |
user.name |
用户的账户名称 |
user.home |
用户的主目录 |
user.dir |
用户的当前工作目录 |
1.1测试代码
package system;
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());
}
}
}
1.2.测试结果
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
2.数组操作
src
引用的源数组到 dest
引用的目标数组,数组组件的一个子序列被复制下来。被复制的组件的编号等于length
参数。源数组中位置在
srcPos
到 srcPos+length-1
之间的组件被分别复制到目标数组中的destPos
到
destPos+length-1
位置。src
- 源数组。srcPos
- 源数组中的起始位置。dest
- 目标数组。destPos
- 目标数据中的起始位置。length
- 要复制的数组元素的数量。 只要下列任何情况为真,则抛出 IndexOutOfBoundsException
异常,并且不会修改目标数组:
-
srcPos
参数为负。 -
destPos
参数为负。 -
length
参数为负。 -
srcPos+length
大于src.length
,即源数组的长度。 -
destPos+length
大于dest.length
,即目标数组的长度。
2.1测试代码
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);
}
}
}
2.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)
3.获取时间
此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。返回值表示从某一固定但任意的时间算起的毫微秒数(或许从以后算起,所以该值可能为负)。此方法提供毫微秒的精度,但不是必要的毫微秒的准确度。它对于值的更改频率没有作出保证。在取值范围大于约 292 年(263 毫微秒)的连续调用的不同点在于:由于数字溢出,将无法准确计算已过的时间。
3.1测试代码
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) + "纳秒");
}
}
3.2测试结果
耗时:1000毫秒
耗时:999958536纳秒