java中Properties类的使用

2016-02-19 20:05 7 1 收藏

关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享java中Properties类的使用教程,希望对大家能有一点小小的帮助。

【 tulaoshi.com - 编程语言 】

  

package com.adrop.util;

?XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" /> 

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

   * @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,"");

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  }

 

  /**

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

   * @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();

  }

}

  
  


来源:http://www.tulaoshi.com/n/20160219/1622706.html

延伸阅读
标签: ASP
  方式一:把Java类注册成ActiveX组件 步骤一: 创建一个Java类,命名为Test.java public class Test {       public String getWord()       {           return "Hello World";       } } 步骤二: 编译成c...
标签: Java JAVA基础
早在Java 1.2推出之时,Java平台中就引入了一个新的支持:java.lang.ThreadLocal,给我们在编写多线程程序时提供了一种新的选择。使用这个工具类可以很简洁地编写出优美的多线程程序,虽然ThreadLocal非常有用,但是似乎现在了解它、使用它的朋友还不多。 ThreadLocal是什么 ThreadLocal是什么呢?其实ThreadLoc...
      最近在维护一个java工程,在群里面也就聊起来java的优劣!无奈一些Java的终极粉丝,总是号称性能已经不必C++差,并且很多标准类库都是大师级的人写的,如何如何稳定等等。索性就认真研究一番,他们给我的一项说明就是,在线程之间投递消息,用java已经封装好的BlockingQueue,就足够用了。    &...
枚举特点 1.用enum定义枚举类默认继承了java.lang.Enum类而不是继承了Object类。其中java.lang.Enum类实现了java.lang.Serializable和java.lang.Comparable两个接口 2.枚举类的构造函数只能使用private访问修饰符,如果省略了其构造器的访问控制符,则默认使用private修饰; 3.枚举类的所有实例必须在枚举类中显式列出,否则这个枚举类将...
  Abstractclass和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstractclass和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进行抽象类定义时对于abstractclass和interface的选择显得比较随意。其实,两者之间...

经验教程

457

收藏

73
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部