属性文件读写(java)

时间:2011-09-24 10:02:18
【文件属性】:

文件名称:属性文件读写(java)

文件大小:3KB

文件格式:JAVA

更新时间:2011-09-24 10:02:18

属性文件读写 javaMail

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(); } return is; } /* * 得到文件输出句柄 */ private OutputStream getPropertyFileOutputStream(){ OutputStream os = null; try { if (checkPropertyFile()) { os = new FileOutputStream(this.propertiesFileName); } } catch (FileNotFoundException e) { e.printStackTrace(); } return os; } /* * 检查属性文件是否被设置值 */ private boolean checkPropertyFile() { if (this.propertiesFileName == null) { return false; } return true; } /** * @MethodName : main * @Author : xuebailong(xuebailong@fenet.com.cn) * @CreateTime : 2008-7-18下午04:36:58 * @Description: * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PropertiyFileReader pfR = new PropertiyFileReader(); pfR.setPropertiesFileName("C:/TEMP/mail.properties"); String email = pfR.getValueByPropertyKey("name"); System.out.println(email); String key = "name"; String value = "xbl"; pfR.setValue(key, value, "This is my name "); } }


网友评论

  • 不实用,还不如直接文件流读写,或是xml读写
  • 不实用,还不如直接文件流读写,或是xml读写