應用場景java
有些時候項目中會用到不少路徑,而且極可能多個路徑在同一個根目錄下,那爲了方便配置的修改,達到只修改根目錄便可達到一改全改的效果,此時就會想到要是有變量就行了;app
另外有時候路徑中的文件名是不肯定的,要靠業務程序運行時去判斷文件名應該如何設置,而又但願此文件下的目錄名是肯定的,那此時用變量也是比較好的解決方式。工具
示例代碼oop
config.properties 以下測試
#根目錄 root_path = D:/myroot #用戶目錄 users_path = ${root_path}/users #用戶相冊目錄 pictures_path = ${users_path}/{username}/pictures/{year}/{month}/{date} #用戶文件目錄 files_path = ${users_path}/{username}/files/{year}/{month}/{date} #系統日誌文件 logs_path = ${root_path}/logs
說明:${root_path}形式的做爲配置文件中路徑變量;{year}形式的做爲運行時替換的字段。this
PropertiesUtil 工具類以下,用於配置文件值和轉化變量spa
package com.tz.util; import java.io.IOException; import java.io.InputStream; import java.util.Calendar; import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author tianzhen * */ public enum PropertiesUtil { ROOT_PATH("root_path"), USERS_PATH("users_path"), PICTURES_PATH("pictures_path"), FILES_PATH("files_path"), LOGS_PATH("logs_path"); private String title; private static Properties props; private PropertiesUtil(String title){ this.title = title; } private static final String PROPERTIES = "config.properties"; private static final Pattern PATTERN = Pattern.compile("\\$\\{([^\\}]+)\\}"); static{ try { props = new Properties(); InputStream ins = PropertiesUtil.class.getClassLoader().getResourceAsStream(PROPERTIES); props.load(ins); ins.close(); } catch (IOException e) { e.printStackTrace(); } } private String getTitle() { return title; } public static String getTranslate(PropertiesUtil prop, Map<String, String> params){ String path = get(prop); if(path==null||path.equals("")){ return ""; } if(params!=null && !params.keySet().isEmpty()){ //須要替換的字段均放入map中,包括須要替換的日期 for(String key : params.keySet()){ path = path.replace("{"+key+"}", params.get(key)); } } //如不指定日期,則替換爲當前日期 Calendar cal = Calendar.getInstance(); path = path.replace("{year}", cal.get(Calendar.YEAR)+"") .replace("{month}", cal.get(Calendar.MONTH)+1>9?cal.get(Calendar.MONTH)+1+"":"0"+(cal.get(Calendar.MONTH)+1)) .replace("{date}", cal.get(Calendar.DATE)>9?cal.get(Calendar.DATE)+"":"0"+cal.get(Calendar.DATE)); return path; } public static String get(PropertiesUtil prop){ String value = props.getProperty(prop.getTitle()); return value==null?null:loop(value); } @SuppressWarnings("static-access") private static String loop(String key){ //定義matcher匹配其中的路徑變量 Matcher matcher = PATTERN.matcher(key); StringBuffer buffer = new StringBuffer(); boolean flag = false; while (matcher.find()) { String matcherKey = matcher.group(1);//依次替換匹配到的路徑變量 String matchervalue = props.getProperty(matcherKey); if (matchervalue != null) { matcher.appendReplacement(buffer, matcher.quoteReplacement(matchervalue));//quoteReplacement方法對字符串中特殊字符進行轉化 flag = true; } } matcher.appendTail(buffer); //flag爲false時說明已經匹配不到路徑變量,則不須要再遞歸查找 return flag?loop(buffer.toString()):key; } }
測試代碼以下日誌
package com.tz.test; import java.util.HashMap; import java.util.Map; import com.tz.util.PropertiesUtil; public class Test { public static void main(String[] args) { System.out.println("********無參取路徑***********"); System.out.println(" [root_path] "+PropertiesUtil.get(PropertiesUtil.ROOT_PATH)); System.out.println(" [users_path] "+PropertiesUtil.get(PropertiesUtil.USERS_PATH)); System.out.println("[pictures_path] "+PropertiesUtil.get(PropertiesUtil.PICTURES_PATH)); System.out.println(" [files_path] "+PropertiesUtil.get(PropertiesUtil.FILES_PATH)); System.out.println(" [logs_path] "+PropertiesUtil.get(PropertiesUtil.LOGS_PATH)); System.out.println("\n********無參轉化取路徑***********"); System.out.println(" [root_path] "+PropertiesUtil.getTranslate(PropertiesUtil.ROOT_PATH , null)); System.out.println(" [users_path] "+PropertiesUtil.getTranslate(PropertiesUtil.USERS_PATH, null)); System.out.println("[pictures_path] "+PropertiesUtil.getTranslate(PropertiesUtil.PICTURES_PATH, null)); System.out.println(" [files_path] "+PropertiesUtil.getTranslate(PropertiesUtil.FILES_PATH, null)); System.out.println(" [logs_path] "+PropertiesUtil.getTranslate(PropertiesUtil.LOGS_PATH, null)); System.out.println("\n********帶參轉化取路徑***********"); Map<String, String> map = new HashMap<String, String>(); map.put("username", "tianzhen"); map.put("year", "2017"); map.put("month", "01"); map.put("date", "02"); System.out.println(" [root_path] "+PropertiesUtil.getTranslate(PropertiesUtil.ROOT_PATH , map)); System.out.println(" [users_path] "+PropertiesUtil.getTranslate(PropertiesUtil.USERS_PATH, map)); System.out.println("[pictures_path] "+PropertiesUtil.getTranslate(PropertiesUtil.PICTURES_PATH, map)); System.out.println(" [files_path] "+PropertiesUtil.getTranslate(PropertiesUtil.FILES_PATH, map)); System.out.println(" [logs_path] "+PropertiesUtil.getTranslate(PropertiesUtil.LOGS_PATH, map)); } }
結果截圖:code