`

Properties文件-增删改查

 
阅读更多

Properties文件-增删改查【转载】

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_);
 }
}
 
分享到:
评论

相关推荐

    篮球球员信息管理系统---增删改查,更新.zip

    【篮球球员信息管理系统---增删改查,更新.zip】是一个基于人工智能技术的项目实践,它主要涉及信息管理系统的设计与实现,使用了Java编程语言。在这个系统中,核心功能包括球员信息的增加、删除、修改和查询,这些...

    SpringBoot 增删改查实例Demo

    在本实例中,我们将深入探讨如何使用SpringBoot框架进行数据库操作,主要集中在增删改查(CRUD)功能。SpringBoot以其简洁的配置和强大的功能,成为Java开发中广泛采用的框架,尤其在构建微服务时更为突出。下面,...

    页面的增删改查代码-java

    本压缩包文件“页面的增删改查代码-java”提供了Java实现的CRUD功能示例,配合SQL数据库,对于学习或开发具有数据管理需求的项目非常有帮助。 首先,让我们详细了解一下CRUD的含义: 1. **创建(Create)**:这是向...

    springBoot简单后台增删改查实例

    在本实例中,我们将深入探讨如何使用SpringBoot框架构建一个简单的后台系统,实现数据库的增删改查功能。SpringBoot以其简洁的配置和强大的功能,成为现代Java开发中的热门选择。接下来,我们将按照标题和描述,详细...

    springboot增删改查

    本知识点将深入探讨如何利用SpringBoot实现数据库的增删改查操作。 一、SpringBoot入门 SpringBoot的核心理念是“约定优于配置”,它内置了Tomcat服务器,自动配置了Spring MVC、Spring Data JPA等模块,使得开发者...

    neo4j服务端开发示例,采用spring-boot开发,内含节点增删改查、关系增删改查、复杂关系的查询。

    在本示例中,我们将深入探讨如何利用Spring Boot框架进行Neo4J服务端开发,实现节点与关系的增删改查以及复杂的查询操作。 首先,"neo4j-community-3.5.18-windows.zip"是Neo4j 3.5.18版本的Windows安装文件。这个...

    对property文件增删改查

    对property类型的配置文件进行增删该查的方法

    JEECG 平台实现增删改查以及将项目部署到服务器流程

    在IT行业中,JEECG平台是一个基于代码生成器的Java快速开发框架,它极大地提高了开发效率,特别是对于常见的增删改查(CRUD)操作。本文将深入探讨如何利用JEECG实现这些基本功能,并详细讲解如何将项目部署到服务器...

    Maven+SpringBoot+JPA单表增删改查实例

    本项目选择了"Maven+SpringBoot+JPA"这一技术栈,旨在提供一个简单的单表操作示例,帮助开发者快速理解如何在Spring Boot环境下使用Maven构建项目,并通过Spring Data JPA实现数据的增删改查功能。 首先,让我们...

    一套基于SpringBoot+SSM框架的增删改查模板

    【标题】"基于SpringBoot+SSM框架的增删改查模板"是一个为初学者设计的项目模板,它结合了Spring、SpringMVC和MyBatis(SSM)这三个核心组件,提供了完整的CRUD(创建、读取、更新、删除)功能。这个模板可以帮助...

    IDEA搭建SSM实现登录、注册,增删改查功能

    创建一个UserRepository接口,继承JpaRepository,定义增删改查的方法。 现在,我们要创建Service层。创建UserService接口和其实现类,这里我们将处理业务逻辑,比如用户登录验证、注册验证等。同时,注入...

    springboot+mybatis+mysql+layUI+thymeleaf实现增删改查

    在本项目中,我们利用了SpringBoot、MyBatis、MySQL、LayUI和Thymeleaf这五种核心技术,构建了一个完整的Web应用程序,实现了学生信息管理系统的增删改查功能。下面将对这些技术及其在项目中的应用进行详细解释。 *...

    基于SSM框架的Java企业级增删改查(CRUD)系统源码

    本项目是一个基于SSM(Spring、SpringMVC、MyBatis)框架的Java企业级增删改查(CRUD)系统源码。该系统主要采用Java语言开发,同时整合了CSS、JavaScript等前端技术。 技术构成: - 后端语言:Java - 前端技术:...

    swing 写的与数据库的增删改查操作

    在"Swing写的与数据库的增删改查操作"中,我们探讨的是如何使用Swing来构建用户界面,同时通过Java Database Connectivity (JDBC) API进行数据库的CRUD(Create、Read、Update、Delete)操作。这个项目可能涉及了...

    数据库ORM框架-GreenDao增删改查Demo

    **数据库ORM框架-GreenDao增删改查Demo** 在Android应用开发中,数据持久化是不可或缺的一部分,而GreenDao作为一款高效、轻量级的ORM(Object-Relational Mapping)框架,使得Android开发者可以更方便地操作SQLite...

    Gradle+SpringBoot+JPA单表的增删改查实例

    在本项目中,我们主要探讨的是如何利用Gradle构建工具,SpringBoot框架,以及Java Persistence API(JPA)来实现单表的增删改查功能。这是一个非常适合初学者上手的实例,它将帮助你理解如何在实际开发环境中集成...

    spring boot mybatis jsp前端展示对mysql增删改查

    在本项目中,我们主要探讨的是如何利用Spring Boot、MyBatis和JSP技术来实现对MySQL数据库的增删改查操作,并通过前端界面展示这些功能。这是一个基础的Web应用程序开发示例,适合初学者用来了解和实践相关技术。...

    SpringBoot整合MyBatis Plus实现增删改查教程

    SpringBoot整合MyBatis Plus实现增删改查教程 本文将详细介绍如何使用 Spring Boot 框架和 MyBatis Plus 实现增删改查(CRUD)操作的教程。MyBatis Plus 是一个基于 MyBatis 的 ORM 框架,它提供了许多实用的功能来...

    greenDAO3 项目搭建与增删改查操作

    **greenDAO3 项目搭建与增删改查操作** greenDAO 是一个流行的对象关系映射(ORM)库,专门用于Android平台,它使得在Android应用中处理SQLite数据库变得更加简便。greenDAO3是其第三个主要版本,引入了一些改进和...

    greendao 实现增删改查功能

    本篇将深入讲解如何利用 greendao 实现增删改查(CRUD:Create, Read, Update, Delete)功能,并基于 `Android Studio` 开发环境进行实践。 1. **安装与配置 greendao** 首先,我们需要在 `build.gradle` 文件中...

Global site tag (gtag.js) - Google Analytics