工具類,關於手工讀取 properties檔案引數

kelelipeng發表於2024-09-26

import java.io.*;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;


/**
*
*
* @Package: [cn.com.wind.Infrastructure.Core.Utils]
* @ClassName: [PropertiesUtil]
* @Description:
* [通用屬性檔案操作]
* @Author: [pli.kelelipeng]
* @Date: [2020年1月8日,上午9:49:21]
* @Version [V1.0]
*
*/
public class PropertiesUtil {

private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

private static Properties props;
static{
loadProps();
}

synchronized static private void loadProps(){
SequenceInputStream si = null;
props = new Properties();
try {
//SequenceInputStream合併多個InputStream 注意第一個檔案的最後一行如果沒有換行符或者註釋會和下一個檔案第一個並列一行

Vector<InputStream> v = new Vector<>(3);
v.addElement(PropertiesUtil.class.getResourceAsStream("/url-param.properties"));
v.addElement(PropertiesUtil.class.getResourceAsStream("/jdbc.properties"));
v.addElement(PropertiesUtil.class.getResourceAsStream("/windAuth.properties"));

Enumeration<? extends InputStream> e = v.elements();
si = new SequenceInputStream(e);
props.load(si);
} catch (FileNotFoundException e) {
logger.error("jdbc.properties檔案未找到");
} catch (IOException e) {
logger.error("出現IOException");
} finally {
try {
if(null != si) {
si.close();
}
} catch (IOException e) {
logger.error("jdbc.properties檔案流關閉出現異常");
}
}

}

public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
}

public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}

/**
* 返回資原始檔的 Properties 物件.
*
* @param strFile
* 傳入屬性檔名稱串.
* @return Properties 物件
* @throws IOException
*/
public static Properties getProperties(String strFile) {
// 類裝載器,僅讀取類路徑下檔案
InputStream inStream = ClassLoader.getSystemResourceAsStream(strFile);
Properties properties = new Properties();
try {
properties.load(inStream);
return properties;
} catch (IOException e) {
String errorInfo=LoggerHelper.getStackErrorInfos(e);
LoggerHelper.LogError.error(errorInfo);
}
return null;
}




public static String getResourcePath2() {
try
{
return ResourceUtils.getURL("classpath:").getPath();
}
catch(Exception ex)
{
return null;
}
}


//在 resource目錄下必須要存在的一個檔名
private static final String resouceFile="application.properties";
private static final String resouceFile2="application.yml";


/**
*
* @Title: getResourcePath
* @Description:
* 資原始檔路徑
* @param: @return
* @return: String
* @throws
*/
public static String getResourcePath() {


String classPath=null;
try {

File file=null;
try
{
//.properties
file= new ClassPathResource(resouceFile).getFile();
}
catch(Exception e)
{
}

if(file==null)
{
//.yml
file= new ClassPathResource(resouceFile2).getFile();
}
classPath = file.getParent();

} catch (IOException e) {
e.printStackTrace();
}
return classPath;
}

}


相關文章