当前位置: 移动技术网 > IT编程>开发语言>Java > Java 操作Properties配置文件详解

Java 操作Properties配置文件详解

2019年07月22日  | 移动技术网IT编程  | 我要评论
1 简介: jdk提供的java.util.properties类继承自hashtable类并且实现了map接口,是使用一种键值对的形式来保存属性集,其中键和值都是字符串

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties

文件中,可以用"#"来作注释,properties文件在java编程中用到的地方很多,操作很方便。

一、properties文件

test.properties
------------------------------------------------------
#################################
# 工商报表应用icisreport的配置文件#
# 日期:2006年11月21日 #
#################################
#
# 说明:业务系统topicis和报表系统icisreport是分离的
# 可分开部署到不同的服务器上,也可以部署到同一个服务
# 器上;icisreprot作为独立的web应用程序可以使用任何
# 的servlet容器或者j2ee服务器部署并单独运行,也可以
# 通过业务系统的接口调用作为业务系统的一个库来应用.
#
# icisreport的ip
icisreport.server.ip=192.168.3.143
# icisreport的端口
icisreport.server.port=8080
# icisreport的上下文路径
icisreport.contextpath=/icisreport

------------------------------------------------------

properties类的重要方法

properties类存在于胞java.util 中,该类继承自 hashtable
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 () ,清除所有装载的 键 - 值对。该方法在基类中提供。
-------------------------------

二、操作properties文件的java方法

读属性文件
properties prop = new properties();
inputstream in = getclass().getresourceasstream("/icisreport.properties");
prop.load(in);
set keyvalue = prop.keyset();
for (iterator it = keyvalue.iterator(); it.hasnext();)
{
string key = (string) it.next();
}
------------------------
outputfile = new fileoutputstream(filename);
propertie.store(outputfile, description);
outputfile.close();
-----------------------------------------------------------------------------------------
class.getresourceasstream ("/some/pkg/resource.properties");
classloader.getresourceasstream ("some/pkg/resource.properties");
java.util.resourcebundle rs = java.util.resourcebundle.getbundle("some.pkg.resource");
rs.getstring("xiaofei");
-----------------------------------------------------------------------------------------
写属性文件
configuration savecf = new configuration();
savecf.setvalue("min", "10");
savecf.setvalue("max", "1000");
savecf.savefile(".\config\save.perperties","test");

总结:java的properties文件需要放到classpath下面,这样程序才能读取到,有关classpath实际上就是java类或者库的存放路径,在java工程中,properties放到class文件一块。在web应用中,最简单的方法是放到web应用的web- inf\classes目录下即可,也可以放在其他文件夹下面,这时候需要在设置classpath环境变量的时候,将这个文件夹路径加到 classpath变量中,这样也也可以读取到。在此,你需要对classpath有个深刻理解,classpath绝非系统中刻意设定的那个系统环境变量,web-inf\classes其实也是,java工程的class文件目录也是。

发个例子大家自己看哈.

package control;

import java.io.bufferedinputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.enumeration;
import java.util.properties;

public class testmain {
 
 //根据key读取value
 public static string readvalue(string filepath,string key) {
 properties props = new properties();
    try {
     inputstream in = new bufferedinputstream (new fileinputstream(filepath));
     props.load(in);
     string value = props.getproperty (key);
      system.out.println(key+value);
      return value;
    } catch (exception e) {
     e.printstacktrace();
     return null;
    }
 }
 
 //读取properties的全部信息
  public static void readproperties(string filepath) {
   properties props = new properties();
    try {
     inputstream in = new bufferedinputstream (new fileinputstream(filepath));
     props.load(in);
      enumeration en = props.propertynames();
       while (en.hasmoreelements()) {
       string key = (string) en.nextelement();
          string property = props.getproperty (key);
          system.out.println(key+property);
        }
    } catch (exception e) {
     e.printstacktrace();
    }
  }

  //写入properties信息
  public static void writeproperties(string filepath,string parametername,string parametervalue) {
   properties prop = new properties();
   try {
   inputstream fis = new fileinputstream(filepath);
      //从输入流中读取属性列表(键和元素对)
      prop.load(fis);
      //调用 hashtable 的方法 put。使用 getproperty 方法提供并行性。
      //强制要求为属性的键和值使用字符串。返回值是 hashtable 调用 put 的结果。
      outputstream fos = new fileoutputstream(filepath);
      prop.setproperty(parametername, parametervalue);
      //以适合使用 load 方法加载到 properties 表中的格式,
      //将此 properties 表中的属性列表(键和元素对)写入输出流
      prop.store(fos, "update '" + parametername + "' value");
    } catch (ioexception e) {
     system.err.println("visit "+filepath+" for updating "+parametername+" value error");
    }
  }

  public static void main(string[] args) {
   readvalue("info.properties","url");
    writeproperties("info.properties","age","21");
    readproperties("info.properties" );
    system.out.println("ok");
  }

发个例子大家自己看哈.

package control;

import java.io.bufferedinputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.enumeration;
import java.util.properties;

public class testmain {
 
 //根据key读取value
 public static string readvalue(string filepath,string key) {
 properties props = new properties();
    try {
     inputstream in = new bufferedinputstream (new fileinputstream(filepath));
     props.load(in);
     string value = props.getproperty (key);
      system.out.println(key+value);
      return value;
    } catch (exception e) {
     e.printstacktrace();
     return null;
    }
 }
 
 //读取properties的全部信息
  public static void readproperties(string filepath) {
   properties props = new properties();
    try {
     inputstream in = new bufferedinputstream (new fileinputstream(filepath));
     props.load(in);
      enumeration en = props.propertynames();
       while (en.hasmoreelements()) {
       string key = (string) en.nextelement();
          string property = props.getproperty (key);
          system.out.println(key+property);
        }
    } catch (exception e) {
     e.printstacktrace();
    }
  }

  //写入properties信息
  public static void writeproperties(string filepath,string parametername,string parametervalue) {
   properties prop = new properties();
   try {
   inputstream fis = new fileinputstream(filepath);
      //从输入流中读取属性列表(键和元素对)
      prop.load(fis);
      //调用 hashtable 的方法 put。使用 getproperty 方法提供并行性。
      //强制要求为属性的键和值使用字符串。返回值是 hashtable 调用 put 的结果。
      outputstream fos = new fileoutputstream(filepath);
      prop.setproperty(parametername, parametervalue);
      //以适合使用 load 方法加载到 properties 表中的格式,
      //将此 properties 表中的属性列表(键和元素对)写入输出流
      prop.store(fos, "update '" + parametername + "' value");
    } catch (ioexception e) {
     system.err.println("visit "+filepath+" for updating "+parametername+" value error");
    }
  }

  public static void main(string[] args) {
   readvalue("info.properties","url");
    writeproperties("info.properties","age","21");
    readproperties("info.properties" );
    system.out.println("ok");
  }
}

到此这篇关于java 操作properties配置文件详解的文章就介绍到这了,更多相关java 操作properties配置文件内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网