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);
}
}
windows, linus读取properties配置文件
最新推荐文章于 2022-03-14 16:47:56 发布