config.properties文件內容如下:
userName=\u5F20\u4E09
password=19920720
java代碼如下:
package com.pifeng.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import org.springframework.core.io.support.PropertiesLoaderUtils;
/**
* @function java讀取config.properties文件
* @author 皮鋒
* @date 2016-1-16
*/
public class PropertiesUtils {
public static String userName = null;
public static String password = null;
/**
* @date 2016/8/11
* @author 皮鋒
* @fun 使用spring提供的工具類讀取.properties文件
*/
private static void springPropUtilReadProp() {
try {
Properties props=PropertiesLoaderUtils.loadAllProperties("config.properties");
userName = props.getProperty("userName");
password = props.getProperty("password");
props.setProperty("function", "springPropUtilReadProp()");// 往屬性文件插值
System.out.println(userName);
System.out.println(password);
System.out.println("function:"+props.getProperty("function"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2016/8/11
* @author 皮鋒
* @fun 使用Properties類讀寫Properties屬性文件,用IO流的方式
*/
private static void streamReadProp() {
Properties properties = new Properties();// 屬性集合對象
InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");// 獲取路徑並轉換成流
try {
properties.load(path);// 將屬性文件流裝載到Properties對象中
userName = properties.getProperty("userName");
password = properties.getProperty("password");
properties.setProperty("function", "streamReadProp()");// 往屬性文件插值
System.out.println(userName);
System.out.println(password);
System.out.println("function:"+properties.getProperty("function"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2016/8/11
* @author 皮鋒
* @fun 讀取資源屬性文件(properties),然後根據.properties文件的名稱信息(本地化信息)
*/
private static void resourceBundleReadProp() {
ResourceBundle resourceBundle = ResourceBundle.getBundle("config");
userName = resourceBundle.getString("userName");
password = resourceBundle.getString("password");
System.out.println(userName);
System.out.println(password);
}
public static void main(String[] args) {
//1.讀取資源屬性文件(properties),然後根據.properties文件的名稱信息(本地化信息)
resourceBundleReadProp();
//2.使用Properties類讀寫Properties屬性文件,用IO流的方式
streamReadProp();
//3.使用spring提供的工具類讀取.properties文件
springPropUtilReadProp();
}
}
運行結果:
張三
19920720
張三
19920720
function:streamReadProp()
張三
19920720
function:springPropUtilReadProp()
新版本:
package com.transfar.base.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.commons.lang3.StringUtils;
import com.transfar.base.factory.LogFactory;
/**
* @func java讀取.properties文件
* @author 皮鋒
* @date 2018/01/04
*/
public class PropertiesUtils {
/**
* @date 2018/01/04
* @author 皮鋒
* @func 讀取資源屬性文件(properties),無緩存方式
*/
public static String readPropertyNoCache(String filePath, String param) {
try {
String url = PropertiesUtils.class.getResource("/").getPath() + filePath;
Properties prop = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(url));
// 將屬性文件流裝載到Properties對象中
// prop.load(in);
prop.load(new InputStreamReader(in, "utf-8"));
return prop.getProperty(param);
} catch (IOException e) {
LogFactory.getLogger().error("讀properties屬性文件異常!", e);
}
return null;
}
/**
* @date 2016/08/11
* @author 皮鋒
* @func 讀取資源屬性文件(properties),用IO流的方式
*/
public static String readProperty(String filePath, String param) {
// 屬性集合對象
Properties properties = new Properties();
// 獲取路徑並轉換成流
InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
try {
// 將屬性文件流裝載到Properties對象中
properties.load(path);
return properties.getProperty(param);
} catch (IOException e) {
LogFactory.getLogger().error("讀properties屬性文件異常!", e);
}
return null;
}
/**
* @date 2016/08/11
* @author 皮鋒
* @func 讀取資源屬性文件(properties),然後根據.properties文件的名稱信息(本地化信息)
*/
public static String getProperty(String filePath, String param) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(filePath);
return resourceBundle.getString(param);
}
/**
*
* @description 讀取.properties配置文件的內容至Map中
* @param propertiesFile
* @param param
* @return map
*/
public static Map<String, String> read2Map(String propertiesFile, String param) {
ResourceBundle rb = ResourceBundle.getBundle(propertiesFile);
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> enu = rb.getKeys();
while (enu.hasMoreElements()) {
String obj = enu.nextElement();
// 傳了參數
if (StringUtils.isNotEmpty(param)) {
if (obj.indexOf(param) != -1) {
String objv = rb.getString(obj);
map.put(obj, objv);
}
}
// 沒傳參數
else {
String objv = rb.getString(obj);
map.put(obj, objv);
}
}
return map;
}
/**
* @func 寫properties文件
* @param filePath
* @param pKey
* @param pValue
*/
public static boolean writeProperties(String filePath, String pKey, String pValue) {
try {
String url = PropertiesUtils.class.getResource("/").getPath() + filePath;
Properties prop = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(url));
// 將屬性文件流裝載到Properties對象中
prop.load(in);
// 調用 Hashtable 的方法 put。使用 getProperty 方法提供並行性。
// 強制要求爲屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
OutputStream out = new FileOutputStream(url);
prop.setProperty(pKey, pValue);
// 以適合使用 load 方法加載到 Properties 表中的格式,
// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
prop.store(out, "Update " + pKey + " name");
return true;
} catch (Exception e) {
LogFactory.getLogger().error("寫properties屬性文件異常!", e);
return false;
}
}
public static void main(String[] args) {
// 1.讀取資源屬性文件(properties),然後根據.properties文件的名稱信息(本地化信息)
String result1 = getProperty("conf/db", "jdbc_url");
System.out.println(result1);
// 2.使用Properties類讀Properties屬性文件,用IO流的方式
String result2 = readProperty("conf/db.properties", "jdbc_url");
System.out.println(result2);
// 寫Properties文件
writeProperties("conf/db.properties", "jdbc_url", "jdbc:mysql://localhost:3306/test");
// 3.使用無緩存的方式讀取Properties屬性文件
String result3 = readPropertyNoCache("conf/db.properties", "jdbc_url");
System.out.println(result3);
}
}