Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:有关properties文件 [Re:bbbaby]
ftang



版主


发贴: 214
积分: 38
于 2003-08-26 23:36 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
[得分不因为以下的程式码特别出色,而是印象中大侠多次热心助人:-) --why]

properties 文件可以有jre指定,看看如何指定java的system properties, 就会知道,这里给一个很小巧的propertiesLoader class,基本可以满足所有你需要的。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.*;

public class PropertiesLoader{

/**
* member variables.
**/
private static final String _PROPERTYFILE = "accesspoint.properties";
private static Logger errLog = Logger.getLogger("PropertiesLoader");
private static Properties props;

/**
* private constructor to prevent outside to call.
**/
private PropertiesLoader(){}

/**
* Depricauted
**/
public static String getProperties(){return null;}

/**
* This method gonna return the full file path you define in the accesspoint.properties file.
*
* @param String propertie name
* @return String the propertie file full path you define in the accesspoint.properties file.
**/
public static String getPropertie(String name){
    if(props == null){
      reload();
    }
String rtnValue = null;
rtnValue = props.getProperty(name);
errLog.info("PropertiesLoader::return value: " + rtnValue + " for name: " + name);
return rtnValue;
}

/**
* this method will be called to load all the properties in accesspoint.peropertie file into the propertie object.
* this method only be called when the first user call this class.
**/
public static void reload(){
FileInputStream file = null;

// current working directory
  String workingDir = System.getProperties().getProperty("user.dir");
  String fullPath = (workingDir == null) ? (".\\" + _PROPERTYFILE) : (workingDir + "\\" + _PROPERTYFILE);
  errLog.info("Properties file path is: " + fullPath);

try{
file = new FileInputStream(_PROPERTYFILE);
props = new Properties();
props.load(file);
}catch( FileNotFoundException fe){
   errLog.error("PropertiesLoader::Cann't find the properties file:", fe);
  }catch( IOException ioe){
errLog.error("PropertiesLoader::IO Exception:", ioe);
}catch(Exception e){
errLog.error("Generic Exception Caught: ", e);
}
}

}

有什么问题可以讨论


why edited on 2003-08-27 00:15


最新JDK 5.0 API.chm中文版与大家分享!

话题树型展开
人气 标题 作者 字数 发贴时间
9822 有关properties文件 bbbaby 108 2003-08-26 20:16
8640 Re:有关properties文件 collins 20 2003-08-26 20:24
8918 Re:有关properties文件 bbbaby 32 2003-08-26 20:32
8800 Re:有关properties文件 bbbaby 7 2003-08-26 20:35
9842 Re:有关properties文件 ftang 2570 2003-08-26 23:36
8955 Re:有关properties文件 bbbaby 495 2003-08-26 21:33
8209 Re:有关properties文件 floater 711 2003-08-27 01:20
8392 Re:有关properties文件 bbbaby 36 2003-08-27 08:38
8417 Re:有关properties文件 bbbaby 19 2003-08-27 08:41
8285 Re:有关properties文件 ftang 317 2003-08-27 11:47
8425 Re:有关properties文件 bbbaby 37 2003-08-27 12:50

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923