服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java中properties类的使用

package com.adrop.util;

 

import java.io.*;

import java.util.properties;

import javax.servlet.http.*;

import javax.servlet.*;

import javax.servlet.jsp.*;

 

public class propertiesutil {

  private string filename;

  private properties p;

  private fileinputstream in;

  private fileoutputstream out;

  /**

   * 根据传进的文件名载入文件

   * @param filename string

   */

  public propertiesutil(string filename) {

    this.filename=filename;

    file file = new file(filename);

    try {

      in = new fileinputstream(file);

      p = new properties();

      //载入文件

      p.load(in);

      in.close();

    }

    catch (filenotfoundexception e) {

      system.err.println("配置文件config.properties找不到!!");

      e.printstacktrace();

    }

    catch (exception e) {

      system.err.println("读取配置文件config.properties错误!!");

      e.printstacktrace();

    }

  }

 

  /**

   * 配置文件一律为config.propertities,并且统一放在web应用的根目录下。

   * @return string

   */

  public static string getconfigfile(httpservlet hs) {

 

    return getconfigfile(hs,"config.properties");

  }

  /**

   * servlet中使用,直接用this作为参数,httpservlet类型

   * 根据配置文件名从当前web应用的根目录下找出配置文件

   * @param hs httpservlet

   * @param configfilename string配置文件名字

   * @return string

   */

  public static string getconfigfile(httpservlet hs, string configfilename) {

    string configfile = "";

    servletcontext sc = hs.getservletcontext();

    configfile = sc.getrealpath("/" + configfilename);

    if (configfile == null || configfile.equals("")) {

      configfile = "/" + configfilename;

    }

    return configfile;

  }

  /**

   * jsp中用pagecontext作参数

   * @param hs pagecontext

   * @param configfilename string 配置文件名字

   * @return string

   */

  public static string getconfigfile(pagecontext hs, string configfilename) {

    string configfile = "";

    servletcontext sc = hs.getservletcontext();

    configfile = sc.getrealpath("/" + configfilename);

    if (configfile == null || configfile.equals("")) {

      configfile = "/" + configfilename;

    }

    return configfile;

  }

 

  /**

   * 列出所有的配置文件内容

   */

  public void list() {

    p.list(system.out);

  }

 

  /**

   * 指定配置项名称,返回配置值

   * @param itemname string

   * @return string

   */

  public string getvalue(string itemname){

    return p.getproperty(itemname);

  }

 

  /**

   * 指定配置项名称和默认值,返回配置值

   * @param itemname string

   * @param defaultvalue string

   * @return string

   */

  public string getvalue(string itemname,

                         string defaultvalue){

    return p.getproperty(itemname,defaultvalue);

  }

 

  /**

   * 设置配置项名称及其值

   * @param itemname string

   * @param value string

   */

  public void setvalue(string itemname,string value){

    p.setproperty(itemname,value);

    return;

  }

 

  /**

   * 保存配置文件,指定文件名和抬头描述

   * @param filename string

   * @param description string

   * @throws exception

   */

  public void savefile(string filename,string description)throws exception{

    try {

      file f=new file(filename);

      out

          = new fileoutputstream(f);

      p.store(out, description);//保存文件

      out.close();

    }

    catch (ioexception ex) {

      throw new exception

          ("无法保存指定的配置文件:"+filename);

    }

  }

 

  /**

   * 保存配置文件,指定文件名

   * @param filename string

   * @throws exception

   */

  public void savefile(string filename)

      throws exception {

    savefile(filename,"");

  }

 

  /**

   * 保存配置文件,采用原文件名

   * @throws exception

   */

  public void savefile() throws exception {

    if(filename.length()==0)

      throw new exception

          ("需指定保存的配置文件名");

    savefile(filename);

  }

  /**

   * 删除一个属性

   * @param value string

   */

  public void deletevalue(string value){

    p.remove(value);

  }

  /**

   * main method for test

   * @param args string[]

   */

  public static void main(string[] args) {

    string file = "f://p.properties";

    propertiesutil pu = new propertiesutil(file);

    pu.list();

  }

}

扫描关注微信公众号