`

Java中读写Properties文件的工具类

    博客分类:
  • Java
阅读更多
package com.common.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 
 * 功能描述:动态读取配置文件来加载属性
 * <p>
 * 版权所有:
 * <p>
 * 未经本公司许可,不得以任何方式复制或使用本程序任何部分
 * 
 * @author dengcd 新增日期:2008-10-9
 * @author 你的姓名 修改日期:2008-10-9
 * @since wapportal_manager version(2.0)
 */
public class PropertyReader {

 private static Logger _log = Logger.getLogger(PropertyReader.class);

 private static Hashtable<String, Properties> pptContainer = new Hashtable<String, Properties>();

 /**
  * 
  * 方法用途和描述: 获得属性 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @param key
  *            属性键
  * @return 属性值
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static String getValue(String propertyFilePath, String key) {
  Properties ppts = getProperties(propertyFilePath);
  return ppts == null ? null : ppts.getProperty(key);
 }

 /**
  * 
  * 方法用途和描述: 获得属性文件中Key所对应的值 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径或文件系统中文件路径)
  * @param key
  *            属性的键
  * @param isAbsolutePath
  *            是否为绝对路径:true|false〔即是本地文件系统路径,比如:C:/test.propreties〕<br>
  * <br>
  *            <b>注:</b>不能通过类路径来获取到属性文件,而只知道属性文件的文件系统路径,即文件系统地址则用此方法来获取其中的Key所对应的Value
  * @return key的属性值
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static String getValue(String propertyFilePath, String key,
   boolean isAbsolutePath) {
  if (isAbsolutePath) {
   Properties ppts = getPropertiesByFs(propertyFilePath);
   return ppts == null ? null : ppts.getProperty(key);
  }
  return getValue(propertyFilePath, key);
 }

 /**
  * 
  * 方法用途和描述: 获得属性文件的属性 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @return 属性
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static Properties getProperties(String propertyFilePath) {
  if (propertyFilePath == null) {
   _log.error("propertyFilePath is null!");
   return null;
  }
  Properties ppts = pptContainer.get(propertyFilePath);
  if (ppts == null) {
   ppts = loadPropertyFile(propertyFilePath);
   if (ppts != null) {
    pptContainer.put(propertyFilePath, ppts);
   }
  }
  return ppts;
 }

 /**
  * 
  * 方法用途和描述: 获得属性文件的属性 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径及文件系统路径)
  * @return 属性文件对象 Properties
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static Properties getPropertiesByFs(String propertyFilePath) {
  if (propertyFilePath == null) {
   _log.error("propertyFilePath is null!");
   return null;
  }
  Properties ppts = pptContainer.get(propertyFilePath);
  if (ppts == null) {
   ppts = loadPropertyFileByFileSystem(propertyFilePath);
   if (ppts != null) {
    pptContainer.put(propertyFilePath, ppts);
   }
  }
  return ppts;
 }

 /**
  * 
  * 方法用途和描述: 加载属性 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @return 属性
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 private static Properties loadPropertyFile(String propertyFilePath) {
  java.io.InputStream is = PropertyReader.class
    .getResourceAsStream(propertyFilePath);
  if (is == null) {
   return loadPropertyFileByFileSystem(propertyFilePath);
  }
  Properties ppts = new Properties();
  try {
   ppts.load(is);
   return ppts;
  } catch (Exception e) {
   _log.debug("加载属性文件出错:" + propertyFilePath, e);
   return null;
  }
 }

 /**
  * 
  * 方法用途和描述: 从文件系统加载属性文件 
  * 
  * @param propertyFilePath
  *            属性文件(文件系统的文件路径)
  * @return 属性
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 private static Properties loadPropertyFileByFileSystem(
   final String propertyFilePath) {
  try {
   Properties ppts = new Properties();
   ppts.load(new java.io.FileInputStream(propertyFilePath));
   return ppts;
  } catch (java.io.FileNotFoundException e) {
   _log.error("FileInputStream(\"" + propertyFilePath
     + "\")! FileNotFoundException: " + e);
   return null;
  } catch (java.io.IOException e) {
   _log.error("Properties.load(InputStream)! IOException: " + e);
   return null;
  }
 }

 /**
  * 
  * 方法用途和描述: 对存在的属性文件中添加键值对并保存
  * 
  * @param propertyFilePath
  *            属性文件的路径(包括类路径及文件系统路径)
  * @param htKeyValue
  *            键值对Hashtable
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean setValueAndStore(String propertyFilePath,
   java.util.Hashtable<String, String> htKeyValue) {
  return setValueAndStore(propertyFilePath, htKeyValue, null);
 }

 /**
  * 
  * 方法用途和描述: 对存在的属性文件中添加键值对并保存 
  * 
  * @param propertyFilePath
  *            属性文件的路径(包括类路径及文件系统路径)
  * @param htKeyValue
  *            键值对Hashtable
  * @param storeMsg
  *            保存时添加的附加信息(注释)
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean setValueAndStore(String propertyFilePath,
   java.util.Hashtable<String, String> htKeyValue, String storeMsg) {
  Properties ppts = getProperties(propertyFilePath);

  if (ppts == null || htKeyValue == null) {
   return false;
  }
  ppts.putAll(htKeyValue);
  java.io.OutputStream stream = null;
  try {
   stream = new java.io.FileOutputStream(propertyFilePath);
  } catch (FileNotFoundException e) {
   _log.debug("propertyFilePath = " + propertyFilePath);
   String path = PropertyReader.class.getResource(propertyFilePath)
     .getPath();
   _log.debug("~~~~~~~~path~~~XXX~~~~~" + path);
   try {
    stream = new java.io.FileOutputStream(path);
   } catch (FileNotFoundException e1) {
    _log.error("FileNotFoundException! path=" + propertyFilePath);
    return false;
   }
  }

  if (stream == null) {
   return false;
  }

  try {
   ppts.store(stream, storeMsg != null ? storeMsg
     : "set value and store.");
   return true;
  } catch (java.io.IOException e) {
   e.printStackTrace();
   return false;
  } finally {
   if (stream != null) {
    try {
     stream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

 /**
  * 
  * 方法用途和描述: 创建属性文件 
  * 
  * @param propertyFilePath
  *            要存储属性文件的路径
  * @param htKeyValue
  *            属性文件中的键值对Hashtable
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean createPropertiesFile(String propertyFilePath,
   java.util.Hashtable<String, String> htKeyValue) {
  java.io.File file = new java.io.File(propertyFilePath);
  if (!file.exists()) {
   try {
    file.createNewFile();
   } catch (java.io.IOException e) {
    e.printStackTrace();
   }
  }
  return setValueAndStore(propertyFilePath, htKeyValue,
    "create properties file:" + file.getName());
 }

 /**
  * 
  * 方法用途和描述:设置属性值 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @param key
  *            属性键
  * @param value
  *            属性值
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean setValue(String propertyFilePath, String key,
   String value) {
  Properties ppts = getProperties(propertyFilePath);
  if (ppts == null) {
   return false;
  }
  ppts.put(key, value);
  return true;
 }

 /**
  * 
  * 方法用途和描述: 保存属性文件对象 
  * 
  * @param properties
  *            属性文件对象
  * @param propertyFilePath
  *            要保存的路径
  * @param msg
  *            保存时添加的附加信息(注释)
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static void store(Properties properties,
   String propertyFilePath, String msg) {
  try {
   java.io.OutputStream stream = new java.io.FileOutputStream(
     propertyFilePath);
   properties.store(stream, msg);
  } catch (java.io.FileNotFoundException e) {
   _log.error("FileOutputStream(" + propertyFilePath
     + ")! FileNotFoundException: " + e);
  } catch (java.io.IOException e) {
   _log.error("store(stream, msg)! IOException: " + e);
   e.printStackTrace();
  }
 }

 /**
  * 
  * 方法用途和描述: 删除属性值 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @param key
  *            属性键
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static String removeValue(String propertyFilePath, String key) {

  Properties ppts = getProperties(propertyFilePath);
  if (ppts == null) {
   return null;
  }
  return (String) ppts.remove(key);
 }

 /**
  * 
  * 方法用途和描述: 删除属性文件中的Key数组所对应的键值对 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径及文件系统路径)
  * @param key
  *            key数组
  * @return 属性文件对象
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static Properties removeValue(String propertyFilePath,
   String[] key) {
  if (key == null) {
   _log.error("key[] is null!");
   return null;
  }
  Properties ppts = getProperties(propertyFilePath);
  if (ppts == null) {
   return null;
  }
  for (String strKey : key) {
   ppts.remove(strKey);
  }
  return ppts;
 }

 /**
  * 
  * 方法用途和描述:删除属性文件中的Key数组所对应的键值对,并将属性文件对象持久化(即保存)
  * 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径及文件系统路径)
  * @param key
  *            属性文件中的key数组
  * @return 成功与否(true|false)
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean removeValueAndStore(String propertyFilePath,
   String[] key) {
  Properties ppts = removeValue(propertyFilePath, key);
  if (ppts == null) {
   return false;
  }
  store(ppts, propertyFilePath, "batch remove key value!");
  return true;
 }

 /**
  * 
  * 方法用途和描述: 更新指定路径的属性文件中的键所对应的值 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径及文件系统路径)
  * @param key
  *            属性文件中的key
  * @param newValue
  *            要更新的新值
  * @return 成功与否(true|false)
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean updateValue(String propertyFilePath,
   String key, String newValue) {
  if (key == null || newValue == null) {
   _log.error("key or newValue is null!");
   return false;
  }
  java.util.Hashtable<String, String> ht = new java.util.Hashtable<String, String>();
  ht.put(key, newValue);
  return setValueAndStore(propertyFilePath, ht, "update " + key
    + "'s value!");
 }

 /**
  * 
  * 方法用途和描述: 批量更新指定路径的属性文件中的键所对应的值 
  * 
  * @param propertyFilePath
  *            属性文件路径(包括类路径及文件系统路径)
  * @param htKeyValue
  *            要更新的键值对Hashtable
  * @return 成功与否(true|false)
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static boolean batchUpdateValue(String propertyFilePath,
   java.util.Hashtable<String, String> htKeyValue) {
  if (propertyFilePath == null || htKeyValue == null) {
   return false;
  }
  return setValueAndStore(propertyFilePath, htKeyValue,
    "batch update key value!");
 }

 /**
  * 
  * 方法用途和描述: 移除加载的属性文件 
  * 
  * @param propertyFilePath
  *            属性文件(包括类路径)
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static Properties removePropertyFile(String propertyFilePath) {

  return pptContainer.remove(propertyFilePath);
 }

 /**
  * 
  * 方法用途和描述: 重新加载某个Property文件 
  * 
  * @param propertyFilePath
  *            要重新加载的Property文件,如果当前内存中没有的话则加载,否则替换
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static void reloadPropertyFile(String propertyFilePath) {
  pptContainer.remove(propertyFilePath);
  loadPropertyFile(propertyFilePath);
 }

 /**
  * 
  * 方法用途和描述: 获得属性文件的路径 
  * 
  * @param pkg
  *            包名
  * @param propertyFileName
  *            属性文件名
  * @return
  * @author dengcd 新增日期:2008-10-9
  * @author 你的姓名 修改日期:2008-10-9
  * @since wapportal_manager version(2.0)
  */
 public final static String getPpropertyFilePath(String pkg,
   String propertyFileName) {

  pkg = pkg == null ? "" : pkg.replaceAll("\\.", "/");
  propertyFileName = propertyFileName.endsWith(".properties") ? propertyFileName
    : (propertyFileName + ".properties");
  return "/" + pkg + "/" + propertyFileName;
 }

 public static void main(String[] args) {
  String path = "/config/jdbc.properties";
  String v = PropertyReader.getValue(path, "jdbc.driverClassName");
  _log.info("value0 = " + v);
  
  
  Hashtable<String, String> ht = new Hashtable<String, String>();
  ht.put("name", "dengcd");
  PropertyReader.setValueAndStore(path, ht);
  String v_ = PropertyReader.getValue(path, "name");
  _log.info("value1 = " + v_);
  PropertyReader.reloadPropertyFile(path);
  String v2_ = PropertyReader.getValue(path, "name");
  _log.info("value2 = " + v2_);
 }
}

 

分享到:
评论
1 楼 momantang 2010-09-07  
未经本公司许可,不得以任何方式复制或使用本程序任何部分

相关推荐

    Java源码读写Properties文件.rar

    压缩包中的“读写Properties文件”可能是包含一个或多个Java源文件,演示了如何使用上述方法读取和写入Properties文件。你可以通过查看这些源码来更好地理解如何实际操作Properties文件。 总结来说,Java中的...

    读取properties文件工具类

    在工具类中,使用`loadProperties`方法加载文件,如`Properties props = PropertiesUtil.loadProperties("config.properties")`。 3. 调用`getProperty`方法获取特定键对应的值,如`String username = ...

    java读写xxx.properties文件实用小例

    在Java编程中,读写`.properties`文件是一个常见的任务,这些文件通常用于存储配置信息、设置或环境变量。本文将深入探讨如何在Java中高效地处理`.properties`文件,包括读取、写入以及更新其内容。我们将参考提供的...

    java配置文件修改读取工具类,包括yml和properties类型文件

    总结来说,`YmlUtil.java`和`PropertiesUtil.java`是Java开发中的实用工具类,它们简化了YAML和Properties配置文件的读取和修改过程,提高了代码的可维护性和灵活性。理解和使用这些工具类,对于提升Java项目管理...

    Python实现读取Properties配置文件的方法

    在Python编程中,有时我们需要处理Java开发中常用的`.properties`配置文件。虽然Python标准库并未直接提供处理此类文件的模块,但我们可以自定义一个类来实现这个功能。本篇文章将详细探讨如何通过Python来读取并...

    Properties文件读写;Property文件读写;Property

    在本案例中,我们关注的是`PropertyUtil.java`工具类,它提供了方便的方法来读取和写入这些Properties文件,简化了开发人员的工作流程。 `PropertyUtil.java`的核心功能可能包括以下几点: 1. **读取Properties...

    java 文件读写

    在实际开发中,为了提高代码的健壮性和易用性,通常会封装这些操作到自定义的工具类中,或者使用Apache Commons IO库等第三方库。例如,`UpdateManifest.java`可能就是一个处理`manifest.properties`文件更新的类,...

    java中读写Properties属性文件公用方法详解

    以下是对Java中读写Properties属性文件公用方法的详细解释: 1. `getProperty(String key)`:这个方法用于根据给定的键`key`查找并返回对应的值。如果找不到匹配的键,将会返回`null`。例如,如果我们有一个键`...

    java读取properties配置文件

    在Java中,我们可以使用`java.util.Properties`类来加载和操作这类文件。以下是读取`properties`文件的基本步骤: 1. 加载`properties`文件: - 创建`Properties`对象实例:`Properties prop = new Properties();`...

    Java Swing操作properties文件

    在实际开发中,通常会将properties文件的读写操作封装到单独的类或方法中,以保持代码的整洁和模块化。此外,为了提高用户体验,可以使用异步处理,避免因文件操作导致的UI冻结。在处理异常时,确保提供有意义的错误...

    excel与properties文件相互转换

    Excel是一种广泛用于数据处理和分析的电子表格工具,而Properties文件则常见于Java开发中,用于存储配置信息。两者之间的相互转换能提升工作效率,特别是在处理大量配置数据时。 Excel文件(.xlsx或.xls)是...

    JAVA读取properties文件的值

    总结,Java中的`Properties`类是读取和管理`.properties`文件的关键工具。通过使用它,开发者可以方便地管理和使用配置信息,同时还可以利用Spring框架提供的`@ConfigurationProperties`实现更高级的绑定功能。正确...

    使用java读写properties文件属性

     我们要做的第一步是要将文件读取到Properties类对象中,由于load有一个参数是InputStream,所以我们可以用 InputStream的子类FileInputStream将属性文件读取到Properties对象中,知道prop.properties的路径,我们...

    java对properties文件的操作

    综上所述,Java中的`Properties`类为处理配置文件提供了强大而方便的工具,无论是简单的键值对存储还是复杂的项目配置,都能轻松应对。在实际开发中,合理使用Properties文件能提高代码的可维护性和灵活性。

    java操作properties方法

    在Java编程中,操作配置文件,尤其是`.properties`文件,是一项常见的任务。`.properties`文件主要用于存储应用程序的配置信息,通常包含键值对,其中键是唯一的标识符,值是与该键相关联的数据。Java提供了`java....

    读取以及修改properties文件

    在Java编程中,Properties文件是用于存储配置信息的文本文件,通常以.properties为扩展名。这些文件包含了应用程序运行时所需的键值对,如数据库连接字符串、API密钥或系统设置等。本篇将深入探讨如何读取和修改...

    自己封装的一些文件(夹)操作和txt文件读写的工具类 哈哈(ExtUtil0.2)

    在这个名为"哈哈(ExtUtil0.2)"的项目中,作者提供了一些自定义的工具类,专门用于简化这些常见的文件和文件夹操作,同时包含了对TXT文件的读写功能。下面我们将详细探讨这些知识点。 首先,工具类(Tool Class)...

    Java 读写Properties配置文件详解

    Java中的Properties类是处理配置文件的关键工具,它用于存储应用程序的配置信息,这些信息通常以键值对的形式存在,键和值都是字符串类型。Properties类继承了Hashtable类,并且实现了Map接口,这意味着它可以像Map...

    Java常用工具类大全,工作5年精心整理.zip

    "Java常用工具类大全,工作5年精心整理.zip"这个压缩包文件很可能包含了一位有经验的Java开发者在五年工作中积累的各种实用工具类,这些工具类能够极大地提高开发效率,简化代码编写。以下是对可能包含的知识点进行...

    java 动态修改配置文件

    本文将深入探讨如何在Java中实现动态修改配置文件,同时解决中文字符编码问题,使得配置文件的读写更加高效和便捷。 首先,我们需要理解Java中的Properties类,它是处理配置文件的标准工具。`java.util.Properties`...

Global site tag (gtag.js) - Google Analytics