windows, linus读取properties配置文件

本文详细解析了Job配置与JDBC配置的加载流程,包括默认路径设置、系统环境判断及路径选择,以及如何通过代码初始化并获取配置属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.sf.opoms.common;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadJobProperties {

//	private final static String CONFIG_FILE_DEFAULT_PATH="/jobConfig.properties";
	
	//job的配置文件名
	private final static String CONFIG_FILE_NAME = "opoms_Config.properties";
	//jdbc的配置文件名
	private final static String JDBC_FILE_NAME = "opoms_Jdbc.properties";
	
	//默认的 dev 本地开发job的配置文件地址    E:/Workspace/opoms/opoms_properties
	private final static String CONFIG_FILE_DEFAULT_PATH_W="E:/Workspace/opoms/opoms_properties/" + CONFIG_FILE_NAME;
	//默认的生产环境job的配置文件地址
	private final static String CONFIG_FILE_DEFAULT_PATH_L="/etc/opoms/" + CONFIG_FILE_NAME;
	
	//默认的dev 本地开发jdbc的配置文件地址 
	private final static String JDBC_FILE_DEFAULT_PATH_W="E:/Workspace/opoms/opoms_properties/" + JDBC_FILE_NAME;		
	//默认的生产环境jdbc的配置文件地址
	private final static String JDBC_FILE_DEFAULT_PATH_L="/etc/opoms/" + JDBC_FILE_NAME;
	
	
	public static String path = null;
	
	private static Properties prop = null;

	public static synchronized void init(){
		prop = new Properties();
		InputStream in = null;
		try {
//			in = Object.class.getResourceAsStream(CONFIG_FILE_DEFAULT_PATH);
			
			//如果main函数的参数传进来的path不为空,则使用path作为路径读取配置文件
			if(path!=null && !path.equals("")){
				in = new BufferedInputStream(new FileInputStream(path+CONFIG_FILE_NAME));
			}else{//如果path为空 则使用代码中默认的路径
				//区分系统的类型是 windows 还是 linux 分别加载不同路径的配置文件
				if(System.getProperty("os.name").startsWith("Windows")){
					in = new BufferedInputStream (
							new FileInputStream(CONFIG_FILE_DEFAULT_PATH_W));				
				}else{
					in = new BufferedInputStream (
							new FileInputStream(CONFIG_FILE_DEFAULT_PATH_L));
				}
			}
			prop.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getProperty(String s) {
		if(prop==null){
			init();
		}
		return prop.getProperty(s);
	}


	private static Properties prop_jdbc = null;

	public static synchronized void init_jdbc(){
		prop_jdbc = new Properties();
		InputStream in = null;
		try {
			//如果main函数的参数传进来的path不为空,则使用path作为路径读取配置文件
			if(path!=null && !path.equals("")){
				in = new BufferedInputStream(new FileInputStream(path+JDBC_FILE_NAME));
			}else{//如果path为空 则使用代码中默认的路径			
				//区分系统的类型是 windows 还是 linux 分别加载不同路径的配置文件
				if(System.getProperty("os.name").startsWith("Windows")){
					in = new BufferedInputStream (
							new FileInputStream(JDBC_FILE_DEFAULT_PATH_W));				
				}else{
					in = new BufferedInputStream (
							new FileInputStream(JDBC_FILE_DEFAULT_PATH_L));
				}
			}
			prop_jdbc.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getProperty_jdbc(String s) {
		if(prop_jdbc==null){
			init_jdbc();
		}
		return prop_jdbc.getProperty(s);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值