package com.xiaoma.util;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class SysUtil {
static String resourceconfigPath = "";
public static Properties getProperties(String filefullname) {
FileInputStream fis = null;
try {
File file = new File(filefullname);
Properties properties = new Properties();
fis = new FileInputStream(file);
properties.load(fis);
return properties;
} catch (Exception e) {
// logger.error(e);
return null;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
return null;
}
}
}
public static String getResourceconfigPath() {
if (resourceconfigPath.equals("")) {
String classPath = SysUtil.class.getClassLoader()
.getResource(SysUtil.class.getName().replace('.', '/') + ".class").getFile();
String classRoot = "";
classRoot = classPath.substring(0, classPath.indexOf("/com/xiaoma/bank/util"));
if (!classRoot.equals("") && classRoot.startsWith("/") && classRoot.indexOf(":") == 2) {
classRoot = classRoot.substring(1);
}
// this.getClass().getClassLoader().getResource("rule.xml").getPath();
if (!classRoot.equals("")) {
resourceconfigPath = classRoot + "/resource.properties";
}
}
return resourceconfigPath;
}
public static String getResourceValue(String key) {
Properties resourceProperties = SysUtil.getProperties(getResourceconfigPath());
return resourceProperties.getProperty(key);
}
}
//第一种 可以放在 自己的定义的路径下
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class dfsfd{
private static Properties props = new Properties();
static {
try {
InputStream in = WebConf.class.getResourceAsStream("/appConfig.properties");
props.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
try {
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(WebConf.getValue("serviceBaseUrl"));
}
}
//第二种 放在 resource 下
package com.xiaoma.utils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
public class Constants {
private static String configFile = "/config.properties";
public static String unionPay_pinBlockKey;
static {
Properties properties = new Properties();
InputStream in = null;
String value = null;
String fieldName = null;
String fieldType = null;
Method method = null;
try {
in = Constants.class.getResourceAsStream(configFile);
properties.load(in);
Class<Constants> clazz = Constants.class;
Constants obj = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
fieldName = field.getName();
if (properties.containsKey(fieldName)) {
method = Properties.class.getDeclaredMethod("getProperty", String.class);
value = (String) method.invoke(properties, fieldName);
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
if (null != value) {
fieldType = field.getType().getName();
if (String.class.getName().equals(fieldType)) {
field.set(obj, value);
} else if ("int".equals(fieldType)) {
field.setInt(obj, Integer.parseInt(value));
} else if ("boolean".equals(fieldType)) {
field.setBoolean(obj, Boolean.parseBoolean(value));
} else {
}
}
}
}
} catch (Exception e) {
System.err.println("载入配置文件" + configFile + "时发生错误");
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 私有构造函数,仅允许Constants在初始化成员变量时创建实例,不允许其他类调用
*/
private Constants() {
}
}
第三种也得放在 resource 下 property 文件