import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties;
public class PropertiesUtil { private final static String propertiesPath ="./config.properties"; private static Properties properties; static{ try{ properties = new Properties(); properties.load(new FileInputStream(propertiesPath)); }catch (Exception e) { e.printStackTrace(); } } public static String get(String key) { return (String)properties.get(key); }
public static void set(String key,String value) { try{ //in.close(); OutputStream os = new FileOutputStream(propertiesPath,false); properties.setProperty(key, value); properties.store(os, "Update '"+key+"' value"); os.close(); }catch (IOException e) { System.out.println("属性文件更新错误"); } } public static void main(String[] args) { System.out.println(get("WeatherDbPasswd")); } }