Web项目的自定义属性配置文件读取- Windows/Linux

将这两个类粘贴到项目中:
获得应用程序路径类:

package com.rayoo.util;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.log4j.Logger;

public class FileTool {
private static Logger logger = Logger.getLogger("com.auib.util.FileTool");

public static String getAppPath(){
//获得应用程序路径
URL appUrl = FileTool.class.getClassLoader().getResource("");
String appPath = appUrl.getPath();

if (WINDOWS && appPath.startsWith("/"))
appPath = appPath.substring(1);

try {
appPath = URLDecoder.decode(appPath, "UTF-8");
}
catch (UnsupportedEncodingException e) { }

return appPath;
}

private static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
}


读取配置文件单例类:

package com.rayoo.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
* 读取属性文件的配置参数
*/
public class ServerConfig {
//private static Logger logger = Logger.getLogger("product run.trns.core.parse.NodeParser");
// 属性配置
private static Properties log = null;
// 单例模式
private static ServerConfig config = null;
// 判断当前系统环境
private static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
// 私有构造
private ServerConfig(){ }

// 单例模式初始化
public static ServerConfig getInstance(){
if(config == null){
config = new ServerConfig();
init();
}
return config;
}

// 初始化函数
private static void init(){
if (log == null){
String configfile = null;
if(WINDOWS)
configfile = "server-config-w.properties";
else
configfile = "server-config-l.properties";

String appPath = FileTool.getAppPath();
File file = new File(appPath);
appPath = file.getParent();
String configPath = appPath;
appPath= appPath + File.separator + configfile;
file = new File(appPath);
FileInputStream fis = null;
try{
if (!file.exists()){
System.out.println("配置文件不存在");
System.exit(-1);
}

fis = new FileInputStream(file);
log = new Properties();
log.load(fis);
log.setProperty("ConfigPath", configPath);
fis.close();
}
catch(Exception e){
//logger.error("config::init::cann't load log file" + e.getMessage());
System.exit(-1);
}
}
}


public Properties getWorkInfo(){
return log;
}

public String getProperty(String key){
return log.getProperty(key);
}

/*
public synchronized void writeLog(String key, String value) throws Exception{
setProperty(key, value);
try{
save();

}
catch(Exception e){
throw new Exception(e.getMessage());
}
}
*/
/*
private void setProperty(String key, String value){
log.setProperty(key, value);
}

private void save() throws Exception{
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
log.store(fos, "");
fos.close();
}
catch(Exception e){
throw new Exception(e.getMessage());
}
}
*/


}


将windows和linux配置文件放到WEB-INF目录下:
windows环境文件:
server-config-w.properties
linux环境配置文件:
server-config-l.properties

获取属性值的代码:

String value = ServerConfig.getInstance().getProperty("key");


或:只要配置的属性文件在类路径下,则可通过如下代码获取:

String configFile = "server-config.properties";
InputStream is = JdbcUtilSingleton.class.getClassLoader().getResourceAsStream(configFile);
Properties props = new Properties();
props.load(is);


获得属性文件的绝对路径:

File iniFile = new File(Holiday.class.getClassLoader().getResource("holiday.properties").getPath());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值