/**********************************本方法只适用于windows操作系统********************************
* 从jar外部获取配置信息
* @param path
* 文件路径及文件名 格式为 aa/bb/path.properties
* @param name
* 需要获取的属性名
* @return
*/
public static String urlPort(String path, String name) {
try {
Properties prop = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream("../" + path));
prop.load(in);
String property2 = prop.getProperty("BDTJ_TIMER");
System.out.println(property2);
// Enumeration<?> propertyNames = prop.propertyNames();
// Object object = prop.get("");
// System.out.println(object.toString());
// while(propertyNames.hasMoreElements()) {
// String key = (String) propertyNames.nextElement();
// String property = prop.getProperty(key);
// System.out.println(key+" "+property);
// }
return property2;
} catch (Exception e) {
}
return null;
}
/********************************此方法适用于windows和lunix********************************
public class PathTest {
public static void main(String[] args) throws Exception {
// new PathTest().getClass().getProtectionDomain().getCodeSource().getLocation();
System.out.println(readPort("JG_TIMER"));
}
/**
* 从jar外部获取配置信息
* @param path
* 文件路径及文件名 格式为 /aa/bb/path.properties (根目录为jar所在目录)
* @param name
* 需要获取的属性名
* @return
*/
public static String readPort(String name) {
String property = System.getProperty("user.dir");
try {
Properties prop = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(property+"port.properties"));
prop.load(in);
String value = prop.getProperty(name);
if (null==value || value.equals("")) {
System.out.println("配置文件中不存在该属性");
}
return value;
} catch (Exception e) {
System.out.println("获取配置文件出错,请检查路径");
e.printStackTrace();
}
return null;
}
}
/*************************************第三种方式*******************************************
全部为 jar路径以及jar包名称:
URL location = new ReadPortUtil().getClass().getProtectionDomain().getCodeSource().getLocation();