ResourceUtils讀取properties文件
阿新 • • 發佈:2017-09-16
audit 系統 n) for 通過 語言 ise sys perfect
註意:
properties文件要放在classPath下面,也就是與src下。
path.properties
perfectMaterial=fileLibrary/completeFile/perfectMaterial perfectMaterialPDF=fileLibrary/completeFile/perfectMaterialPDF promiseFile=fileLibrary/originalFile/promiseFile signatureFile=signature signatureBackupFile=signatureBackup investigatePicture=fileLibrary/originalFile/investigatePicture auditPicture=fileLibrary/originalFile/auditPicture auditReprt=fileLibrary/originalFile/auditReprt sevenMaterial=fileLibrary/originalFile/sevenMaterial sevenMaterialPdf=fileLibrary/originalFile/PDFFile/sevenMaterialPdf auditReportWordAndPdf=fileLibrary/originalFile/auditReportWordAndPdf jdbc.test=fileLibrary/originalFile/auditReportWordAndPdf
ResourcesUtil.java
package Utils.ResourceUtil; import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.ResourceBundle;import java.util.Set; /** * 資源文件讀取工具類 * */ public class ResourcesUtil implements Serializable { private static final long serialVersionUID = -7657898714983901418L; /** * 系統語言環境,默認為中文zh */ public static final String LANGUAGE = "zh"; /** * 系統國家環境,默認為中國CN */ public static final String COUNTRY = "CN"; private static Locale getLocale() { Locale locale = new Locale(LANGUAGE, COUNTRY); return locale; } /** * 根據語言、國家、資源文件名和key名字獲取資源文件值 * * @param language * 語言 * * @param country * 國家 * * @param baseName * 資源文件名 * * @param section * key名字 * * @return 值 */ private static String getProperties(String baseName, String section) { String retValue = ""; try { Locale locale = getLocale(); ResourceBundle rb = ResourceBundle.getBundle(baseName, locale); retValue = (String) rb.getObject(section); } catch (Exception e) { e.printStackTrace(); // TODO 添加處理 } return retValue; } /** * 通過key從資源文件讀取內容 * * @param fileName * 資源文件名 * * @param key * 索引 * * @return 索引對應的內容 */ public static String getValue(String fileName, String key) { String value = getProperties(fileName,key); return value; } public static List<String> gekeyList(String baseName) { Locale locale = getLocale(); ResourceBundle rb = ResourceBundle.getBundle(baseName, locale); List<String> reslist = new ArrayList<String>(); Set<String> keyset = rb.keySet(); for (Iterator<String> it = keyset.iterator(); it.hasNext();) { String lkey = (String)it.next(); reslist.add(lkey); } return reslist; } /** * 通過key從資源文件讀取內容,並格式化 * * @param fileName * 資源文件名 * * @param key * 索引 * * @param objs * 格式化參數 * * @return 格式化後的內容 */ public static String getValue(String fileName, String key, Object[] objs) { String pattern = getValue(fileName, key); String value = MessageFormat.format(pattern, objs); return value; } public static void main(String[] args) { System.out.println(getValue("resources.messages", "101",new Object[]{100,200})); //根據操作系統環境獲取語言環境 /*Locale locale = Locale.getDefault(); System.out.println(locale.getCountry());//輸出國家代碼 System.out.println(locale.getLanguage());//輸出語言代碼s //加載國際化資源(classpath下resources目錄下的messages.properties,如果是中文環境會優先找messages_zh_CN.properties) ResourceBundle rb = ResourceBundle.getBundle("resources.messages", locale); String retValue = rb.getString("101");//101是messages.properties文件中的key System.out.println(retValue); //信息格式化,如果資源中有{}的參數則需要使用MessageFormat格式化,Object[]為傳遞的參數,數量根據資源文件中的{}個數決定 String value = MessageFormat.format(retValue, new Object[]{100,200}); System.out.println(value); */ } }
測試代碼
package Utils.ResourceUtil; import org.junit.Test; public class TestUtils { @Test public void Test1(){ System.out.println(ResourcesUtil.getValue("path", "investigatePicture")); System.out.println(ResourcesUtil.getValue("path", "auditReprt")); System.out.println(ResourcesUtil.getValue("path", "jdbc.test")); } }
結果:
fileLibrary/originalFile/investigatePicture
fileLibrary/originalFile/auditReprt
fileLibrary/originalFile/auditReportWordAndPdf
ResourceUtils讀取properties文件