属性文件读写(java)
package org.fenet.javamail; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; /** * @ProjectName : JavaTest * @PackageName : org.fenet.javamail * @FileName : PropertiyFileReader.java * @Describe : * @CreateTime : 2008-9-3下午04:07:01 * @Author : xuebailong(xuebailong@fenet.com.cn) */ public class PropertiyFileReader { /* * 属性文件名, 包括路径 */ private String propertiesFileName = null; private Properties pro = null; public void setPropertiesFileName(String propertiesFileName) { this.propertiesFileName = propertiesFileName; } public PropertiyFileReader() { } public PropertiyFileReader(String propertiesFileName) { this.propertiesFileName = propertiesFileName; } /* * 根据key * 得到属性文件的值 */ public String getValueByPropertyKey(String key) { String value = null; if (checkPropertyFile()) { value = getValue(key); } return value; } /* * get Value */ private String getValue(String key) { this.pro = new Properties(); String value = null; InputStream is = getPropertyFileInputStream(); try { pro.load(is); value = pro.getProperty(key); is.close(); } catch (IOException e) { e.printStackTrace(); } return value; } /* * set Value, * comments : 注释 */ private void setValue(String key, String value, String comments){ this.pro = new Properties(); this.pro.setProperty(key, value); OutputStream os = getPropertyFileOutputStream(); try { pro.store(os, comments); os.close(); } catch (IOException e) { e.printStackTrace(); } } /* * 得到文件输入句柄 */ private InputStream getPropertyFileInputStream() { InputStream is = null; try { if (checkPropertyFile()) { is = new FileInputStream(this.propertiesFileName); } } catch (FileNotFoundException e) { e.printStackTrace(); } ret
用户评论