java 之 Properties 类
import sun.awt.resources.awt; import java.util.Properties; import java.util.Set; /** * Properties 理解为一个键和值都是字符串的Map * extends HashTable [过时,很多方法不建议使用] * 常用功能是作为属性集(如系统环境变量,系统配置等场景) * * 常用方法: * String getProperty(String key) 用指定的键在此属性列表中搜索属性 * String getProperty(String key, String defaultValue) 用指定的键在属性列表中搜索,不存在则返回默认值 * setProperty(String key, String value) 调用HashTable的方法 put * Set<String> stringPropertyNames() 获取所有的键 */ public class PropertyTest { public static void main(String[] args) { propertyTest(); } static void propertyTest(){ Properties properties = System.getProperties(); String key1 = properties.getProperty("JAVA_HOME");//null System.out.println(key1); String key2 = properties.getProperty("JAVA_HOME", "c:"); System.out.println(key2);//c: key1 = properties.getProperty("JAVA_HOME");//null System.out.println(key1); properties.setProperty("JAVA_HOME", "bin"); key1 = properties.getProperty("JAVA_HOME");//bin System.out.println(key1); Set<String> set = properties.stringPropertyNames(); for (String item: set) { System.out.println("item="+item); } /* item=java.runtime.name item=sun.boot.library.path item=java.vm.version item=java.vm.vendor item=java.vendor.url item=path.separator item=java.vm.name item=file.encoding.pkg item=user.script item=user.country item=sun.java.launcher item=sun.os.patch.level item=java.vm.specification.name item=user.dir item=java.runtime.version item=java.awt.graphicsenv item=java.endorsed.dirs item=os.arch item=java.io.tmpdir item=line.separator item=java.vm.specification.vendor item=user.variant item=os.name item=sun.jnu.encoding item=java.library.path item=java.specification.name item=java.class.version item=sun.management.compiler item=os.version item=user.home item=user.timezone item=java.awt.printerjob item=file.encoding item=java.specification.version item=user.name item=java.class.path item=java.vm.specification.version item=sun.arch.data.model item=java.home item=sun.java.command item=java.specification.vendor item=user.language item= awt.toolkit item=java.vm.info item=java.version item=java.ext.dirs item=sun.boot.class.path item=java.vendor item=file.separator item=java.vendor.url.bug item=sun.cpu.endian item=sun.io.unicode.encoding item=JAVA_HOME item=sun.desktop item=sun.cpu.isalist */ } }