欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-08 09:14:27
前言 大家都知道java中有个比较重要的类properties(java.util.properties),主要用于读取java的配置文件,各种语言都有自己所支持的配置文...

前言

大家都知道java中有个比较重要的类properties(java.util.properties),主要用于读取java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类configparse,方便程序员或用户通过该类的方法来修改.ini配置文件。在java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。

它提供了几个主要的方法:

     1. getproperty ( string key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

     2. load ( inputstream instream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getproperty ( string key) 来搜索。

     3. setproperty ( string key, string value)  ,调用 hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

     4. store ( outputstream out, string comments) ,以适合使用 load 方法加载到 properties 表中的格式,将此 properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

     5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

如下示例代码提供了一套读写配置文件的公用实用方法,可以根据自己的项目进行引入:

package com.javacui.lucene.jdbc;
import java.io.bufferedinputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import java.util.enumeration;
import java.util.properties;
import org.apache.log4j.logger;
public class propertieutil {
 private static logger logger = logger.getlogger(propertieutil.class);
 private propertieutil() {
 }
 /**
  * 读取配置文件某属性
  */
 public static string readvalue(string filepath, string key) {
  properties props = new properties();
  try {
   // 注意路径以 / 开始,没有则处理
   if (!filepath.startswith("/"))
    filepath = "/" + filepath;
   inputstream in = propertieutil.class.getresourceasstream(filepath);
   props.load(in);
   string value = props.getproperty(key);
   return value;
  } catch (exception e) {
   logger.error(e);
   return null;
  }
 }
 /**
  * 打印配置文件全部内容(filepath,配置文件名,如果有路径,props/test.properties)
  */
 public static void readproperties(string filepath) {
  properties props = new properties();
  try {
   // 注意路径以 / 开始,没有则处理
   if (!filepath.startswith("/"))
    filepath = "/" + filepath;
   inputstream in = propertieutil.class.getresourceasstream(filepath);
   props.load(in);
   enumeration<?> en = props.propertynames();
   // 遍历打印
   while (en.hasmoreelements()) {
    string key = (string) en.nextelement();
    string property = props.getproperty(key);
    logger.info(key + ":" + property);
   }
  } catch (exception e) {
   logger.error(e);
  }
 }
 /**
  * 将值写入配置文件
  */
 public static void writeproperties(string filename, string parametername, string parametervalue) throws exception {
  // 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
  // 注意路径不能加 / 了,加了则移除掉
  if (filename.startswith("/"))
   filename.substring(1);
  string filepath = propertieutil.class.getresource("/").getpath()+filename;
  // 获取配置文件
  properties pps = new properties();
  inputstream in = new bufferedinputstream(new fileinputstream(filepath));
  pps.load(in);
  in.close();
  outputstream out = new fileoutputstream(filepath);
  // 设置配置名称和值
  pps.setproperty(parametername, parametervalue);
  // comments 等于配置文件的注释
  pps.store(out, "update " + parametername + " name");
  out.flush();
  out.close();
 }
 public static void main(string[] args) throws exception {
  readproperties("jdbc.properties");
//  logger.info(readvalue("jdbc.properties", "javablog_write_url"));
//  writeproperties("test.properties", "test", "test");
 }
}

总结

以上就是关于java读写properties属性文件公用方法的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。