国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

java中Properties類的使用

2019-11-17 05:53:39
字體:
供稿:網(wǎng)友

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;

  /**

   * 根據(jù)傳進(jìn)的文件名載入文件

   * @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錯(cuò)誤!!");

      e.printStackTrace();

    }

  }

 

  /**

   * 配置文件一律為config.propertities,并且統(tǒng)一放在web應(yīng)用的根目錄下。

   * @return String

   */

  public static String getConfigFile(HttpServlet hs) {

 

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

  }

  /**

   * 在servlet中使用,直接用this作為參數(shù),HttpServlet類型

   * 根據(jù)配置文件名從當(dāng)前web應(yīng)用的根目錄下找出配置文件

   * @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作參數(shù)

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

  }

 

  /**

   * 列出所有的配置文件內(nèi)容

   */

  public void list() {

    p.list(System.out);

  }

 

  /**

   * 指定配置項(xiàng)名稱,返回配置值

   * @param itemName String

   * @return String

   */

  public String getValue(String itemName){

    return p.getProperty(itemName);

  }

 

  /**

   * 指定配置項(xiàng)名稱和默認(rèn)值,返回配置值

   * @param itemName String

   * @param defaultValue String

   * @return String

   */

  public String getValue(String itemName,

                         String defaultValue){

    return p.getProperty(itemName,defaultValue);

  }

 

  /**

   * 設(shè)置配置項(xiàng)名稱及其值

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

  }

  /**

   * 刪除一個(gè)屬性

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

  }

}




發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金乡县| 大悟县| 衡山县| 蒲城县| 恩平市| 禹州市| 杨浦区| 昌都县| 商洛市| 浦东新区| 盐源县| 榆林市| 秦安县| 板桥市| 霍林郭勒市| 乌兰察布市| 略阳县| 新郑市| 洛南县| 铜鼓县| 新疆| 开封市| 唐河县| 林甸县| 团风县| 山东| 临朐县| 靖安县| 凉城县| 奉新县| 海安县| 嫩江县| 师宗县| 巫山县| 伊川县| 南郑县| 体育| 内黄县| 清新县| 西盟| 图片|