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

server-config-w.properties 该文件放在src目录下面

 

  1. upload=D:/upload/  
  2. conn_driver=test_1  
  3. ftp_choice_auto=test_2  
  4. ftp_ip=test_3  
  5. ftp_passWord=test_4  
  6. conn_userName=test_5  
  7. ftp_choice_store=test_6  
  8. file_store_path=test_7  
  9. conn_type=test_8  
  10. conn_passWord=test_9  
  11. ftp_userName=test_10  
  12. conn_url=test_11  
  13. ftp_port=test_12  
upload=D:/upload/
conn_driver=test_1
ftp_choice_auto=test_2
ftp_ip=test_3
ftp_passWord=test_4
conn_userName=test_5
ftp_choice_store=test_6
file_store_path=test_7
conn_type=test_8
conn_passWord=test_9
ftp_userName=test_10
conn_url=test_11
ftp_port=test_12

获取应用程序路径类:

 

 

  1. package com.hzzy.pqgl.util;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.net.URL;  
  5. import java.net.URLDecoder;  
  6.   
  7. import org.apache.log4j.Logger;  
  8.   
  9. public class FileTool {  
  10.     private static Logger logger = Logger.getLogger("com.auib.util.FileTool");  
  11.   
  12.     public static String getAppPath() {  
  13.         // 获得应用程序路径   
  14.         URL appUrl = FileTool.class.getClassLoader().getResource("");  
  15.         String appPath = appUrl.getPath();  
  16.   
  17.         if (WINDOWS && appPath.startsWith("/"))  
  18.             appPath = appPath.substring(1);  
  19.   
  20.         try {  
  21.             appPath = URLDecoder.decode(appPath, "UTF-8");  
  22. //          appPath = System.getProperty("user.dir");   
  23.         } catch (UnsupportedEncodingException e) {  
  24.         }  
  25.   
  26.         return appPath;  
  27.     }  
  28.   
  29.     private static final boolean WINDOWS = System.getProperty("os.name")  
  30.             .startsWith("Windows");  
  31. }  
package com.hzzy.pqgl.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");
//			appPath = System.getProperty("user.dir");
		} catch (UnsupportedEncodingException e) {
		}

		return appPath;
	}

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


读取配置文件单例类:

 

 

  1. package com.hzzy.pqgl.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.util.Properties;  
  6.   
  7. /** 
  8.  * 读取属性文件的配置参数 
  9.  */  
  10. public class ServerConfig {  
  11.     //private static Logger logger = Logger.getLogger("product run.trns.core.parse.NodeParser");   
  12.     // 属性配置   
  13.     private static Properties log = null;  
  14.     // 单例模式   
  15.     private static ServerConfig config = null;  
  16.     // 判断当前系统环境   
  17.     private static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");  
  18.     // 私有构造   
  19.     private ServerConfig(){ }  
  20.       
  21.     // 单例模式初始化   
  22.     public static ServerConfig getInstance(){  
  23.         if(config == null){  
  24.             config = new ServerConfig();  
  25.             init();  
  26.         }  
  27.         return config;  
  28.     }  
  29.       
  30.     // 初始化函数   
  31.     private static void init(){  
  32.         if (log == null){  
  33.             String configfile = null;  
  34.             if(WINDOWS)  
  35.                 configfile = "server-config-w.properties";  
  36.             else  
  37.                 configfile = "server-config-l.properties";  
  38.               
  39.             String appPath = FileTool.getAppPath();  
  40.             File file = new File(appPath);  
  41.             String appPathParent = file.getParent();  
  42.             String configPath = appPathParent;  
  43.             appPath= appPath + File.separator + configfile;  
  44.             file = new File(appPath);     
  45.             FileInputStream fis = null;  
  46.             try{  
  47.                 if (!file.exists()){  
  48.                     System.out.println("配置文件不存在");  
  49.                     System.exit(-1);  
  50.                 }  
  51.   
  52.                 fis = new FileInputStream(file);  
  53.                 log = new Properties();  
  54.                 log.load(fis);  
  55.                 //conn_driver=test_1   
  56.                 log.setProperty("ConfigPath", configPath);  
  57.                 fis.close();  
  58.             }  
  59.             catch(Exception e){  
  60.                 //logger.error("config::init::cann't load log file" + e.getMessage());   
  61.                 System.exit(-1);  
  62.             }  
  63.         }  
  64.     }  
  65.       
  66.       
  67.     public Properties getWorkInfo(){  
  68.         return log;  
  69.     }  
  70.       
  71.     public String getProperty(String key){  
  72.         return log.getProperty(key);  
  73.     }  
  74.       
  75.     /* 
  76.     public synchronized void writeLog(String key, String value) throws Exception{ 
  77.         setProperty(key, value); 
  78.         try{ 
  79.             save(); 
  80.              
  81.         } 
  82.         catch(Exception e){ 
  83.             throw new Exception(e.getMessage()); 
  84.         } 
  85.     } 
  86.     */  
  87.     /* 
  88.     private void setProperty(String key, String value){ 
  89.         log.setProperty(key, value); 
  90.     } 
  91.      
  92.     private void save() throws Exception{ 
  93.         FileOutputStream fos = null; 
  94.         try{ 
  95.             fos = new FileOutputStream(file); 
  96.             log.store(fos, ""); 
  97.             fos.close(); 
  98.         } 
  99.         catch(Exception e){ 
  100.             throw new Exception(e.getMessage()); 
  101.         } 
  102.     } 
  103.     */  
  104.       
  105.       
  106. }  
package com.hzzy.pqgl.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);
			String appPathParent = file.getParent();
			String configPath = appPathParent;
			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);
				//conn_driver=test_1
				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());
		}
	}
	*/
	
	
}


测试类:

 

 

    1. package com.hzzy.pqgl.util;  
    2.   
    3. public class Test {  
    4.     public static void main(String args[]) {  
    5.         String value = ServerConfig.getInstance().getWorkInfo()  
    6.                 .getProperty("upload");  
    7.     }  
    8. }  

转载于:https://www.cnblogs.com/sfmjp/articles/2924632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值