如何使用Java读写系统属性?
读:
简述properties文件的结构和基本用法
结构:扩展名为properties的文件,内容为key、value的映射,例如"a=2"
示例用到的properties文件:
test.properties
a=testA
b:testB
package properties; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; public class PropertiesManipulate { public static void main(String[] args) throws IOException { readProperties(); readSystemProperties(); } private static void readSystemProperties() { Properties props = System.getProperties(); Enumeration<?> prop_names = props.propertyNames(); while (prop_names.hasMoreElements()) { String prop_name = (String) prop_names.nextElement(); String property = props.getProperty(prop_name); System.out.println("Property \"" + prop_name + "\" is \"" + property + "\""); } } private static void readProperties() throws FileNotFoundException, IOException { String name = "test.properties"; InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); System.out.println("a==>" + p.getProperty("a")); } }
输出:
a==>testA Property "java.runtime.name" is "Java(TM) SE Runtime Environment"
...(omit)
写:
System.setProperties(props);
http://pda158.iteye.com/blog/2160442