yaml檔案的讀取以及其工具類
阿新 • • 發佈:2019-01-01
由於在工作中會出現一系列以鍵值對形式選項引數對應,可以通過yaml檔案進行讀取檔案流。
type.yml
#計劃稽核表-查詢型別
TCX_JHSBB_AUDIT_searchSblx:
- valueData: 1
displayName: 待稽核
- valueData: 2
displayName: 已稽核
YamlUtil
package com.yufei.core.util.conf; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.yaml.snakeyaml.Yaml; import com.yufei.core.entity.SelectModel; import com.yufei.core.util.PathUtil; /** * type.yml 配置檔案對應下拉集合顯示 * @author Lenovo */ public class YamlUtil { // 計劃稽核表-查詢型別(計劃稽核、配合稽核) public static final String TCX_JHSBB_AUDIT_SEARCHSBLX = "TCX_JHSBB_AUDIT_searchSblx"; // 查詢型別 /** * 讀取下拉狀態配置引數List * @param typeName * @return * @throws IOException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static List<SelectModel> getTypePropertie(String typeName) throws IOException{ String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路徑 File yamlFile = new File(yamlPath); Yaml yaml = new Yaml(); HashMap map = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class); List<SelectModel> list = new ArrayList<SelectModel>(); List contentList = (List)map.get(typeName); for (Object item : contentList){ list.add(new SelectModel(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString())); } return list; } /** * 讀取下拉狀態配置引數返回map * @param typeName * @return * @throws IOException */ public static Map<String,Object> getTypePropertieMap(String typeName) throws IOException{ String yamlPath = PathUtil.getClassResources() + "conf/type.yml"; // yaml路徑 File yamlFile = new File(yamlPath); Yaml yaml = new Yaml(); HashMap hashMap = yaml.loadAs(new FileInputStream(yamlFile), HashMap.class); Map<String,Object> map = new HashMap<String,Object>(); List contentList = (List)hashMap.get(typeName); for (Object item : contentList){ map.put(((HashMap)item).get("valueData").toString(), ((HashMap)item).get("displayName").toString()); } return map; } }
注意:這裡需要匯入一個包,可以在Maven中引入
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
</dependency>
上面中還引入了一個PathUtil的路徑工具類
package com.yufei.core.util; import java.io.File; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * 路徑工具類 * * @author * */ public class PathUtil { /** * 圖片訪問路徑 * * @param pathType * 圖片型別 visit-訪問;save-儲存 * @param pathCategory * 圖片類別,如:話題圖片-topic、話題回覆圖片-reply、商家圖片 * @return */ public static String getPicturePath(String pathType, String pathCategory) { String strResult = ""; HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); StringBuffer strBuf = new StringBuffer(); if ("visit".equals(pathType)) { } else if ("save".equals(pathType)) { String projectPath = PublicUtil.getPorjectPath().replaceAll("\\\\", "/"); projectPath = splitString(projectPath, "bin/"); strBuf.append(projectPath); strBuf.append("webapps/ROOT/"); } strResult = strBuf.toString(); return strResult; } private static String splitString(String str, String param) { String result = str; if (str.contains(param)) { int start = str.indexOf(param); result = str.substring(0, start); } return result; } /* * 獲取classpath1 */ public static String getClasspath(){ String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim(); if(path.indexOf(":") != 1){ path = File.separator + path; } return path; } /* * 獲取classpath2 */ public static String getClassResources(){ String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim(); if(path.indexOf(":") != 1){ path = File.separator + path; } return path; } public static String PathAddress() { String strResult = ""; HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); StringBuffer strBuf = new StringBuffer(); strBuf.append(request.getScheme() + "://"); strBuf.append(request.getServerName() + ":"); strBuf.append(request.getServerPort() + ""); strBuf.append(request.getContextPath() + "/"); strResult = strBuf.toString();// +"ss/";//加入專案的名稱 return strResult; } }
SelectModel
package com.yufei.core.entity; /** * 下拉繫結物件 * @author zhanghd 2017-07-27 * */ public class SelectModel { public SelectModel() { super(); } public SelectModel(String valueData, String displayName) { super(); this.valueData = valueData; this.displayName = displayName; } /** * 下拉值 */ private String valueData; /** * 下拉顯示內容 */ private String displayName; public String getValueData() { return valueData; } public void setValueData(String valueData) { this.valueData = valueData; } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } }
注意:在測試過程中,報了一個Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
原因,type.yml中的指定配置檔案編碼不是UTF-8的,轉換成UTF-8就行了。