标签:
package resource.and.property; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties; public class Main { public static final String THIS_PATH = Main.class.getResource("").getPath(); public static final String CONFIG_FILE = THIS_PATH + "app.properties"; public static final InputStream INPUT = Main.class .getResourceAsStream("app.properties"); public static void main(String[] args) { // getJVMProperties(); getProperties(); } public static void getJVMProperties() { Properties props = System.getProperties(); props.list(System.out); } public static void getProperties() { Properties pps = new Properties(); try { pps.load(new FileInputStream(CONFIG_FILE)); // pps.load(INPUT); } catch (IOException e) { e.printStackTrace(); } @SuppressWarnings("rawtypes") Enumeration enum1 = pps.propertyNames(); while (enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } static class TestProperties { public static String GetValueByKey(String filePath, String key) { Properties pps = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream( filePath)); pps.load(in); String value = pps.getProperty(key); System.out.println(key + " = " + value); return value; } catch (IOException e) { e.printStackTrace(); return null; } } @SuppressWarnings("rawtypes") public static void GetAllProperties(String filePath) throws IOException { Properties pps = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream( filePath)); pps.load(in); Enumeration en = pps.propertyNames(); while (en.hasMoreElements()) { String strKey = (String) en.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } public static void WriteProperties(String filePath, String pKey, String pValue) throws IOException { Properties pps = new Properties(); InputStream in = new FileInputStream(filePath); pps.load(in); OutputStream out = new FileOutputStream(filePath); pps.setProperty(pKey, pValue); pps.store(out, "Update " + pKey + " name"); } public static void main(String[] args) throws IOException { String value = GetValueByKey(CONFIG_FILE, "name"); System.out.println(value); System.out.println("--------------"); GetAllProperties(CONFIG_FILE); System.out.println("--------------"); WriteProperties(CONFIG_FILE, "long", "212"); GetAllProperties(CONFIG_FILE); } } }
一般两种方式获得properties:
pps.load(new BufferedInputStream(new FileInputStream(filePath))); pps.load(Main.class.getResourceAsStream(filePath));
注意:上述代码中各种stream流都未关闭,在实际应用中记得关闭流。
标签:
原文地址:http://www.cnblogs.com/drizzlewithwind/p/5722129.html