今天写一个小程序,需要获取当前项目的根目录。百度搜索得知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 2015java.runtime.name=Java(TM) SE Runtime Environmentsun.boot.library.path=D\:\\InstallFiles\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\binjava.vm.version=11.3-b02java.vm.vendor=Sun Microsystems Inc.java.vendor.url=http\://java.sun.com/path.separator=;java.vm.name=Java HotSpot(TM) Client VMfile.encoding.pkg=sun.iosun.java.launcher=SUN_STANDARDuser.country=CNsun.os.patch.level=java.vm.specification.name=Java Virtual Machine Specificationuser.dir=D\:\\workspace\\study\\2015-3-17Java-FileUpDownjava.runtime.version=1.6.0_13-b03java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironmentjava.endorsed.dirs=D\:\\InstallFiles\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jre\\lib\\endorsedos.arch=x86java.io.tmpdir=C\:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\line.separator=\r\njava.vm.specification.vendor=Sun Microsystems Inc.user.variant=os.name=Windows Vistasun.jnu.encoding=GBKjava.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\\libjava.specification.name=Java Platform API Specificationjava.class.version=50.0sun.management.compiler=HotSpot Client Compileros.version=6.2user.home=C\:\\Users\\Administratoruser.timezone=Asia/Shanghaijava.awt.printerjob=sun.awt.windows.WPrinterJobfile.encoding=GBKjava.specification.version=1.6java.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.jaruser.name=Administratorjava.vm.specification.version=1.0java.home=D\:\\InstallFiles\\Genuitec\\Common\\binary\\com.sun.java.jdk.win32.x86_1.6.0.013\\jresun.arch.data.model=32user.language=zhjava.specification.vendor=Sun Microsystems Inc.awt.toolkit=sun.awt.windows.WToolkitjava.vm.info=mixed modejava.version=1.6.0_13java.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\\extsun.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.jarjava.vendor=Sun Microsystems Inc.file.separator=\\java.vendor.url.bug=http\://java.sun.com/cgi-bin/bugreport.cgisun.io.unicode.encoding=UnicodeLittlesun.cpu.endian=littlesun.desktop=windowssun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
关于这个user.dir,有网友说是用户工作目录,理由就是user.dir是user directory的缩写;我觉得这个说法不对,个人理解user.dir只是一个key而已。