import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Properties; public class propertiesHandle { private Properties m_properties; private static String path = "properties.conf"; public propertiesHandle(){ try { InputStreamReader in = new InputStreamReader(new FileInputStream(path),"GBK"); Properties pro = new Properties(); try { pro.load(in); m_properties = pro; in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 将文件加载到内存,修改key的value值,再保存 */ public void setProperties(String name,String value){ m_properties.setProperty(name, value); try { FileOutputStream out = new FileOutputStream(path); m_properties.store(out, null); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getProperties(String name){ return m_properties.getProperty(name); } public static void main(String args[]){ propertiesHandle pro = new propertiesHandle(); System.out.println(pro.getProperties("host")); } }
配置文件的位置
配置文件内容事例