今天写一个小程序,需要获取当前项目的根目录。百度搜索得知System.getProperty("user.dir")该方法可以做到。看到这行代码不由让我思考:“user.dir”是一个什么玩意?System.getProperty("key")该方法到底有多少个key?于是经过对api的研究,发现了System.getProperties()这个方法可以获取到所有的key值;代码如下:
1 public void getSystemKey(){ 2 3 File sysFile = new File("d:/study","sysFile.properties"); 4 Properties sysPro = System.getProperties(); 5 try { 6 sysPro.store(new FileOutputStream(sysFile), "Properties ClASS"); 7 } catch (FileNotFoundException e1) { 8 e1.printStackTrace(); 9 } catch (IOException e) { 10 e.printStackTrace(); 11 } 12 13 }
得到的sysFile.properties文件内容如下:
#Properties ClASS #Wed Mar 18 16:04:29 CST 2015 java.runtime.name=Java(TM) SE Runtime Environment sun.boot.library.path=D:\InstallFiles\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=D:\workspace\study\2015-3-17Java-FileUpDown java.runtime.version=1.6.0_13-b03 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\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 Vista sun.jnu.encoding=GBK java.library.path=D:\InstallFiles\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:/InstallFiles/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;D:/InstallFiles/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\InstallFiles\dzpk\Java\jdk1.6.0_24\lib 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=Asia/Shanghai java.awt.printerjob=sun.awt.windows.WPrinterJob file.encoding=GBK java.specification.version=1.6 java.class.path=D:\workspace\study\2015-3-17Java-FileUpDown\WebRoot\WEB-INF\classes;D:\workspace\study\2015-3-17Java-FileUpDown\WebRoot\WEB-INF\lib\poi-3.11-20141221.jar;D:\workspace\study\2015-3-17Java-FileUpDown\WebRoot\WEB-INF\lib\commons-logging-1.1.3.jar;D:\workspace\study\2015-3-17Java-FileUpDown\WebRoot\WEB-INF\lib\junit-4.11.jar;D:\workspace\study\2015-3-17Java-FileUpDown\WebRoot\WEB-INF\lib\log4j-1.2.17.jar user.name=Administrator java.vm.specification.version=1.0 java.home=D:\InstallFiles\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:\InstallFiles\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:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\resources.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\rt.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\sunrsasign.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\jsse.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\jce.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\charsets.jar;D:\InstallFiles\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\classes;D:\InstallFiles\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.1.me201010211225\data\libraryset\EE_5\javaee.jar;D:\InstallFiles\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.1.me201010211225\data\libraryset\EE_5\jsf-impl.jar;D:\InstallFiles\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.1.me201010211225\data\libraryset\EE_5\jsf-api.jar;D:\InstallFiles\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.1.me201010211225\data\libraryset\EE_5\jstl-1.2.jar 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
关于这个user.dir,有网友说是用户工作目录,理由就是user.dir是user directory的缩写;我觉得这个说法不对,个人理解user.dir只是一个key而已。