java中Properties类的使用
2008-01-05 20:38:44 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁诡垎鍐f寖闂佺娅曢幑鍥灳閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡欏嚬缂併劎绮妵鍕箳鐎n亞浠鹃梺闈涙搐鐎氫即鐛崶顒夋晬婵絾瀵ч幑鍥蓟閻斿摜鐟归柛顭戝枛椤牆顪冮妶搴′簼缂侇喗鎸搁悾鐑藉础閻愬秵妫冮崺鈧い鎺戝瀹撲礁鈹戦悩鎻掝伀缁惧彞绮欓弻娑氫沪閹规劕顥濋梺閫炲苯澧伴柟铏崌閿濈偛鈹戠€n€晠鏌嶆潪鎷屽厡闁汇倕鎳愮槐鎾存媴閸撴彃鍓卞銈嗗灦閻熲晛鐣烽妷褉鍋撻敐搴℃灍闁绘挻娲橀妵鍕箛闂堟稐绨肩紓浣藉煐濮樸劎妲愰幘璇茬闁冲搫鍊婚ˇ鏉库攽椤旂》宸ユい顓炲槻閻g兘骞掗幋鏃€鐎婚梺瑙勬儗閸樺€熲叺婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閳锋垶銇勯幒鍡椾壕缂備礁顦遍弫濠氱嵁閸℃稒鍊烽柛婵嗗椤旀劕鈹戦悜鍥╃У闁告挻鐟︽穱濠囨嚃閳哄啰锛滈梺褰掑亰閸欏骸鈻撳⿰鍫熺厸閻忕偟纭堕崑鎾诲箛娴e憡鍊梺纭呭亹鐞涖儵鍩€椤掑啫鐨洪柡浣圭墪閳规垿鎮欓弶鎴犱桓闂佸湱枪閹芥粎鍒掗弮鍫熷仺缂佸顕抽敃鍌涚厱闁哄洢鍔岄悘鐘绘煕閹般劌浜惧┑锛勫亼閸婃牠宕濋敃鈧…鍧楀焵椤掍胶绠剧€光偓婵犱線鍋楀┑顔硷龚濞咃絿妲愰幒鎳崇喓鎷犻懠鑸垫毐闂傚倷鑳舵灙婵炲鍏樺顐ゆ嫚瀹割喖娈ㄦ繝鐢靛У绾板秹寮查幓鎺濈唵閻犺櫣灏ㄥ銉р偓瑙勬尭濡繂顫忛搹鍦<婵☆垰鎼~宥囩磽娴i鍔嶉柟绋垮暱閻g兘骞嬮敃鈧粻濠氭偣閸パ冪骇鐎规挸绉撮—鍐Χ閸℃ê闉嶇紓浣割儐閸ㄥ墎绮嬪澶嬪€锋い鎺嶇瀵灝鈹戦埥鍡楃仯闁告鍕洸濡わ絽鍟崐鍨叏濡厧浜鹃悗姘炬嫹

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();
}
}
Tags:java Properties 使用
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接