`

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());
分享到:
评论
1 楼 虎踞龙盘忆江南 2012-08-30  
你有个地方写的有点问题!
            appPath = file.getParent(); 
            String configPath = appPath;
改为:     

            String appPathParent = file.getParent();
    String configPath = appPathParent;

相关推荐

    Linux简明教程.rar

    2.Windows linux双系统 3.只安装 Ubuntu 4.硬盘安装 四、硬件驱动的安装--------------------------------------------------------------------------------- Linux的初体验----------------------------------...

    Beanstalk 队列服务器控制台 Aurora-Console.zip

    macOS/Linux/Windows 32/64-bit- 单文件简单易部署- 不依赖其他组件- 支持读取配置文件方式启动 登陆用户认证- 定时刷新 Beanstalk 队列服务器状态- 对每个 Tube 的 ready/delayed/buried 状态进行管理- 支持批量...

    nServer-v2.1023[FTP + MYSQL + HTTP + PHP(FCGI)]

    - 重建配置文件的时候,自动备份旧配置文件,防止误删 - 开启MySQL性能元数据信息库引擎(performance_schema) - 优化xServer.bat脚本性能 2012年08月30日 - 更新MySQL版本为5.5.27 - 更新FileZilla版本为0.9.41 ...

    aurora-console

    跨平台支持 macOS/Linux/Windows 32/64-bit 单文件简单易部署 不依赖其他组件 支持读取配置文件方式启动 + 登陆用户认证 定时刷新 Beanstalk 队列服务器状态 对每个 Tube 的 ready/delayed/buried 状态进行...

    DataGear数据可视化分析平台

    每一种类型的图表都以图表插件形式提供,并内置了大量图表插件,管理员也可上传自定义图表插件,丰富系统图表类型; 5、可自由编辑的HTML看板模板 看板使用原生的HTML网页作为模板,可自由编辑、绑定、异步加载图表...

    新版Android开发教程.rar

    o Apache Ant 1.6.5 or later for Linux and Mac, 1.7 or later for Windows o Not Not Not Not compatible with Gnu Compiler for Java (gcj) Note: Note: Note: Note: If JDK is already installed on your ...

    KODExplorer 芒果云-资源管理器

    - 优化配置文件存储方案。直接由前端操作后端key,value - 修复添加收藏夹问题(已打开设置窗口再添加失败问题) - 修复树目录中文文件名bug - 优化pic图片幻灯片播放 - 优化新建文件&文件夹 清空选择状态 - ...

    JAVA上百实例源码以及开源项目

     数字证书:从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂…… Java+ajax写的登录实例 1个目标文件 内容索引:Java源码,初学实例,ajax,登录  一个Java+ajax写...

    odroid-Testing-Repo:测试 repo 让社区参与汽车制造商

    示例:自定义应用程序自定义配置 通用/额外包 这些是安装到您的操作系统的额外包,在 customBuild 运行时读取。 普通/htmlHelper 一组用于制作输出 Web 界面的 bash 函数 配置文件夹 这用于构建 linux 内核的...

    IIS6.0 IIS,互联网信息服务

    四、在Vista系统中安装IIS7.0相对于早先的版本,IIS 7.0 带来了许多引人注目的新特色新功能,比如基于 Microsoft .NET Framework 的全局配置文件,可简单地通过文本编辑器或 Microsoft Visual Studio 编辑;...

    JAVA上百实例源码以及开源项目源代码

     数字证书:从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂…… Java+ajax写的登录实例 1个目标文件 内容索引:Java源码,初学实例,ajax,登录  一个Java+ajax写...

    vc++ 开发实例源码包

    实例使用了加载类似xml文件读取信息,然后显示。 COM_ATL_Tutorial 简单的atl控件演示 COM接口挂钩及其应用 如题。 CSkinSlier CSliderCtrl自绘 Cursor 生成图标,运行到鼠标图标。 cutscene win32下实现视频...

    DataGear数据可视化分析平台-其他

    每一种类型的图表都以图表插件形式提供,并内置了大量图表插件,管理员也可上传自定义图表插件,丰富系统图表类型; 5、可自由编辑的HTML看板模板 看板使用原生的HTML网页作为模板,可自由编辑、绑定、异步加载图表...

    网管教程 从入门到精通软件篇.txt

    CCB:Visual Basic动态按钮配置文件 CCH:Corel图表文件 CCO:CyberChat数据文件 CCT:Macromedia Director Shockwave投影 CDA:CD音频轨道 CDF:Microsoft频道定义格式文件 CDI:Philip的高密盘交互格式 CDM...

    NTKO文档在线编辑控件4.0.1.2

    9 广泛的操作系统,Web服务器,数据库和编程语言支持 后台支持Windows,Linux,Unix等各种操作系统;支持IIS,Domino,Websphere,Apache等所有后台WEB服务器类型,支持Db2,Oracle,MySQL,SQL Server等各种常用...

    java开源包1

    [ini4j] 是一个简单的Java类库,用来读写Windows的ini配置文件。同时还包含一个 Java Perferences API 的实现。 拒绝服务测试工具 Port Groper PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的...

Global site tag (gtag.js) - Google Analytics