System类说明
- System类代表当前Java程序的运行平台,程序不能创建System类的对象,所以它提供了一些类属性和类方法,允许直接通过System类名来调用这些属性和方法。
- System类提供了代表标准输入、标准输出和错误输出的类属性;并提供了一些静态方法用于访问环境变量、系统属性的方法。
代码示例
import java.io.*;
import java.util.*;
public class SystemTest {
/**
* @param args
* @throws Exception
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, Exception {
// 获取系统所有的环境变量
Map<String,String> env = System.getenv();
for (String name : env.keySet())
{
System.out.println(name + " ---> " + env.get(name));
}
// 获取指定环境变量的值
System.out.println(System.getenv("JAVA_HOME"));
// 获取所有的系统属性
Properties props = System.getProperties();
// 将所有系统属性保存到props.txt文件中
props.store(new FileOutputStream("props.txt")
, "System Properties");
// 输出特定的系统属性
System.out.println(System.getProperty("os.name"));
}
}
运行结果
USERPROFILE ---> C:\Users\lenovo
ProgramData ---> C:\ProgramData
PATHEXT ---> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
ProgramFiles(x86) ---> C:\Program Files (x86)
TEMP ---> C:\Users\lenovo\AppData\Local\Temp
SystemDrive ---> C:
ProgramFiles ---> C:\Program Files (x86)
Path ---> D:/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;D:/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\Java\jdk1.8.0_101\bin;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;D:\Java\jdk1.8.0_101\bin
HOMEDRIVE ---> C:
PROCESSOR_REVISION ---> 4e03
USERDOMAIN ---> LAPTOP-RL60HVJ9
ALLUSERSPROFILE ---> C:\ProgramData
ProgramW6432 ---> C:\Program Files
PROCESSOR_IDENTIFIER ---> Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
SESSIONNAME ---> Console
FPS_BROWSER_USER_PROFILE_STRING ---> Default
TMP ---> C:\Users\lenovo\AppData\Local\Temp
CommonProgramFiles ---> C:\Program Files (x86)\Common Files
LOGONSERVER ---> \\LAPTOP-RL60HVJ9
PROCESSOR_ARCHITECTURE ---> x86
OS ---> Windows_NT
PROCESSOR_ARCHITEW6432 ---> AMD64
HOMEPATH ---> \Users\lenovo
FPS_BROWSER_APP_PROFILE_STRING ---> Internet Explorer
PROCESSOR_LEVEL ---> 6
CommonProgramW6432 ---> C:\Program Files\Common Files
USERDOMAIN_ROAMINGPROFILE ---> LAPTOP-RL60HVJ9
configsetroot ---> C:\WINDOWS\ConfigSetRoot
LOCALAPPDATA ---> C:\Users\lenovo\AppData\Local
COMPUTERNAME ---> LAPTOP-RL60HVJ9
windir ---> C:\WINDOWS
SystemRoot ---> C:\WINDOWS
NUMBER_OF_PROCESSORS ---> 4
USERNAME ---> lenovo
PUBLIC ---> C:\Users\Public
PSModulePath ---> C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
CommonProgramFiles(x86) ---> C:\Program Files (x86)\Common Files
ComSpec ---> C:\WINDOWS\system32\cmd.exe
APPDATA ---> C:\Users\lenovo\AppData\Roaming
null
Windows Vista
该程序还会在当前路径下生成一个props.txt文件,文件的内容是:
#System Properties
#Mon Aug 29 21:50:25 CST 2016
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\bin
java.vm.version=11.3-b02
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=F\:\\java\\workspace\\first
java.runtime.version=1.6.0_13-b03
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\endorsed
os.arch=x86
java.io.tmpdir=C\:\\Users\\lenovo\\AppData\\Local\\Temp\\
line.separator=\r\n
java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows Vista
sun.jnu.encoding=GBK
java.library.path=D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\bin;.;C\:\\WINDOWS\\Sun\\Java\\bin;C\:\\WINDOWS\\system32;C\:\\WINDOWS;D\:/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;D\:/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C\:\\WINDOWS\\system32;C\:\\WINDOWS;C\:\\WINDOWS\\System32\\Wbem;C\:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;C\:\\Program Files\\Intel\\WiFi\\bin\\;C\:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;D\:\\Java\\jdk1.8.0_101\\bin;C\:\\Users\\lenovo\\AppData\\Local\\Microsoft\\WindowsApps;D\:\\Java\\jdk1.8.0_101\\bin
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\\lenovo
user.timezone=Asia/Shanghai
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=GBK
java.specification.version=1.6
java.class.path=F\:\\java\\workspace\\first\\bin
user.name=lenovo
java.vm.specification.version=1.0
java.home=D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\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_13
java.ext.dirs=D\:\\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\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\resources.jar;D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\rt.jar;D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\sunrsasign.jar;D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\jsse.jar;D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\jce.jar;D\:\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\charsets.jar;D\:\\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.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
代码示例
public class IdentityHashCodeTest {
/**
* @param args
*/
public static void main(String[] args) {
// 下面程序中s1和s2是两个不同对象
String s1 = new String("Hello");
String s2 = new String("Hello");
// String重写了hashCode()方法——改为根据字符序列计算hashCode值,
// 因为s1和s2的字符序列相同,所以它们的hashCode方法返回值相同
System.out.println(s1.hashCode()
+ "----" + s2.hashCode());
// s1和s2是不同的字符串对象,所以它们的identityHashCode值不同
System.out.println(System.identityHashCode(s1)
+ "----" + System.identityHashCode(s2));
String s3 = "Java";
String s4 = "Java";
// s3和s4是相同的字符串对象,所以它们的identityHashCode值相同
System.out.println(System.identityHashCode(s3)
+ "----" + System.identityHashCode(s4));
}
}
运行结果
69609650----69609650
31843011----25860399
5184781----5184781
代码说明
通过identityHashCode(Object o)方法可以获取对象的identityHashCode值,这个特殊的值可以唯一地标识该对象。因为identityHashCode值是根据对象的地址计算得到的,所以任何两个对象的identityHashCode值都是不一样的。