配置文件中,字符串占位符替换

xiaoxiao2021-02-28  120

/** * 配置文件中,替换字符串 * @author Sailing * */ public class PropertiesTest { private static PropertiesTest instance; public static PropertiesTest getInstance(){ if(instance==null){ instance = new PropertiesTest(); } return instance; } private Properties properties = new Properties(); private PropertiesTest(){ try { String fileName = "cfg/pro.properties"; this.properties.load(new FileReader(fileName)); } catch (IOException e) { e.printStackTrace(); } } /** * 获取配置文件 * @param key * @return * @author Sailing */ public String getProperties (String key) { return this.properties.getProperty(key); } /** * 替换字符串 * @param previousStr * @param params * @return * @author Sailing */ public static String replace(String previousStr,String [] params){ String newStr = MessageFormat.format(previousStr, params); return newStr; } public static void main(String[] args) { //加载配置文件 PropertiesTest pro = PropertiesTest.getInstance(); String name = pro.getProperties("name"); String score = pro.getProperties("score"); String[] mathParam = {"math","8"}; String math = replace(score, mathParam); String[] englishParam = {"english","3"}; String english = replace(score, englishParam); System.out.println("Name:"+name+"\r\n"+math+"\r\n"+english); } }
转载请注明原文地址: https://www.6miu.com/read-32705.html

最新回复(0)