第一种方式
Properties properties = new Properties();
try {
properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
//遍历取值
Set<Object> objects = properties.keySet();
for (Object object : objects) {
//取到参数赋值给静态变量
if (object.toString().equals("webservice.uri")) {
BaseUrl = new String(properties.getProperty((String) object));
}
}
} catch (IOException e) {
e.printStackTrace();
}
第二种方式工具类
resource下application路径:src/main/resources/properties/test.properties
package com.scy.dispatch.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class PropertiesUtils {
private static Properties ps = new Properties();
private static String proPath = null;
private static Map<String, String> proLoadMap = null;
public PropertiesUtils(String path) {
// TODO Auto-generated constructor stub
proPath = path;
proLoadMap = getPropertiesMap(path);
}
public PropertiesUtils() {
}
/**
* 获取文件的values值
*
* @param filePath
* @param key
* @return
*/
public static String getPropertiesValue(String filePath, String key) {
if (null != filePath) {
String value = null;
File propertiesFile = new File(filePath);
if (!propertiesFile.exists()) {
return null;
}
Properties properties = new Properties();
FileInputStream propertiesInputStream = null;
try {
propertiesInputStream = new FileInputStream(propertiesFile);
properties.load(propertiesInputStream);
value = properties.getProperty(key).trim();
// System.err.println(value);
propertiesInputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != propertiesInputStream)
propertiesInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (value.isEmpty())
return null;
return value;
} else {
return null;
}
}
/**
* 设置properties文件k,v
*
* @param filePath
* @param key
* @param value
* @return
*/
public static boolean setProertiesConf(String filePath, String key, String value) {
if (null != filePath && null != key && null != value && !key.equals("") && !value.equals("")) {
FileOutputStream out = null;
try {
PropertiesUtils properties = new PropertiesUtils(filePath);
@SuppressWarnings("static-access")
Map<String, String> wen = properties.getPropertiesMap(filePath);
wen.put(key, value);
for (Map.Entry<String, String> entry : wen.entrySet()) {
ps.setProperty(entry.getKey(), entry.getValue());
}
File propertiesFile = new File(filePath);
if (!propertiesFile.exists()) {
return false;
}
out = new FileOutputStream(propertiesFile);
ps.store(out, null);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} else {
return false;
}
}
@SuppressWarnings("unused")
private static boolean mapToProperties(Map<?,?> map) {
FileOutputStream out = null;
for (Map.Entry<String, String> entry : proLoadMap.entrySet()) {
ps.setProperty(entry.getKey(), entry.getValue());
}
File propertiesFile = new File(proPath);
if (!propertiesFile.exists()) {
return false;
}
try {
out = new FileOutputStream(propertiesFile);
ps.store(out, null);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 将properties配置文件转换城map格式
*
* @param propertiesFilePath
* @return Map<String, String>
*/
@SuppressWarnings("rawtypes")
public static Map<String, String> getPropertiesMap(String propertiesFilePath) {
FileInputStream in = null;
File propertiesFile = new File(propertiesFilePath);
if (!propertiesFile.exists()) {
return null;
}
try {
in = new FileInputStream(propertiesFile);
ps.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.getMessage();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
Map<String, String> wen = new HashMap<String, String>((Map) ps);
return wen;
}
/**
* 获取加载好的Properties的Value值
*
* @param key
* @return String
*/
public String getDefaultPropertiesValue(String key) {
if (null != proLoadMap) {
return proLoadMap.get(key);
}
return null;
}
/**
* 获取加载好的Properties的所有Key
*
* @return Set<String>
*/
public Set<String> getDefaultKeys() {
if (null != proLoadMap) {
return proLoadMap.keySet();
}
return null;
}
/**
* 设置已经加载好文件的K和V值
*
* @param key
* @param value
* @return boolean true:成功 false:失败
*/
public boolean put(String key, String value) {
boolean TF = setProertiesConf(proPath, key, value);
proLoadMap = getPropertiesMap(proPath);
return TF;
}
public Properties getDefaultPs() {
return ps;
}
public String getDefaultProPath() {
return proPath;
}
public Map<String, String> updateDefaultProPath(String proPath) {
PropertiesUtils.proPath = proPath;
return proLoadMap = getPropertiesMap(proPath);
}
public Map<String, String> getDefaultProLoadMap() {
return proLoadMap;
}
public void setDefaultProLoadMap(Map<String, String> map) {
proLoadMap = map;
}
public boolean Commit(Map<String, String> map) {
return mapToProperties(proLoadMap);
}
}
1