Java-Properties文件读取工具类

时间:2023-03-09 22:31:41
Java-Properties文件读取工具类
 import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; /**
* @ClassName: ReadPropertiesUtil
*/
public class ReadPropertiesUtil {
// 声明配置文件
private static String[] confProps = { "config.properties" };
private static PropertiesConfiguration conf = null; /**
* 获取所有配置对象
*/
public static PropertiesConfiguration getConf() {
conf = new PropertiesConfiguration();// 初始化对象
/* 把所有props配置文件一次加载,共后续使用 */
for (int i = 0; i < confProps.length; i++) {
try {
conf.load(confProps[i]);
} catch (ConfigurationException e) {
conf = null;
}
}
return conf;
} /*
* PropsUtil getInt()
*/
public static Integer getInt(String key) {
/** 声明返回值 **/
Integer value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getInt(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getString()
*/
public static String getString(String key) {
/** 声明返回值 **/
String value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getString(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getLong()
*/
public static Long getLong(String key) {
/** 声明返回值 **/
Long value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getLong(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getBoolean()
*/
public static boolean getBoolean(String key) {
/** 声明返回值 **/
boolean value = true;
try {
/* 获取value值 */
conf = getConf();
value = conf.getBoolean(key);
} catch (Exception e) {
value = false;
}
return value;
} /*
* PropsUtil getList()
*/
public static String[] getStringArray(String key) {
/** 声明返回值 **/
String[] value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getStringArray(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getJsonHeaders()
*/
public static JSONObject getJsonHeaders(String key) {
JSONObject json = null;// 声明返回值
try {
/** 解析*.Properties文件 **/
conf = getConf();
String value = conf.getString(key).replace("=", ",");
json = JSON.parseObject(value);
} catch (Exception e) {
}
return json;
} public static void main(String[] args) throws Exception { System.out.println(getString("dbcp.url")); // String[] tmp = getStringArray("Email_to");
// for(String to:tmp){
// System.out.println(to);
// } // JSONObject json = getJsonHeaders("httpHead");
// for(Entry<String, Object> entry:json.entrySet()){
// System.out.println(entry.getKey() + "-----" + entry.getValue().toString());
// } // System.out.println(getInt("dbcp.url")); // System.out.println(getInt("redis.minIdle"));
// String[] tmp = getStringArray("redis.redisSlaveIp");
// for(int i=0;i<tmp.length;i++){
// System.out.println(tmp[i].split(":")[0]);
// System.out.println(tmp[i].split(":")[1]);
// }
}
}
 //相关依赖
<!-- commons-configuration -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<!-- fastjson json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>

Properties文件载入工具类:

  依赖spring等。

  

         <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
 import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; /**
* Properties文件载入工具类. 可载入多个properties文件,
* 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
*/
public class PropertiesLoader {
private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties; public PropertiesLoader(String... resourcesPaths) {
properties = loadProperties(resourcesPaths);
} public Properties getProperties() {
return properties;
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
} /**
* 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
} /**
* 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
} /**
* 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
} /**
* 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
} /**
* 取出Property,但以System的Property优先,取不到返回空字符串.
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return "";
} /**
* 载入多个文件, 文件路径使用Spring Resource格式.
*/
private Properties loadProperties(String... resourcesPaths) {
Properties props = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException ex) {
logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
} finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
}