-----------------ResourceBundle讀取配置文件-------------------------------java
下面咱們來模擬一個多語言的環境web
定義四個資源文件:res_en_US.properties、res_zh_CN.properties、res_zh.properties、res.propertiesapp
res_en_US.properties:cancelKey=cancel測試
res_zh_CN.properties:cancelKey=\u53D6\u6D88(取消)
spa
res_zh.properties:cancelKey=\u53D6\u6D88zh(取消zh)
code
res.properties:cancelKey=\u53D6\u6D88default(取消default)
orm
命名規則按照:資源名+_語言_國別.properties,每一個資源文件中定義了本地化的信息,那麼系統如何取到對應的資源文件呢?xml
ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));
其中new Locale("zh", "CN");這個對象就告訴了程序你的本地化信息,就拿這個來講吧:程序首先會去classpath下尋找res_zh_CN.properties對象
若不存在,那麼會去找res_zh.properties,若仍是不存在,則會去尋找res.properties,要仍是找不到的話,那麼就該拋異常了:MissingResourceException資源
咱們能夠來寫個測試程序驗證一下:
package bundle.test; import java.util.Locale; import java.util.ResourceBundle; public class BundleTest { public static void main(String args[]) { ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN")); String cancel = bundle.getString("cancelKey"); System.out.println(cancel); bundle = ResourceBundle.getBundle("res", Locale.US); cancel = bundle.getString("cancelKey"); System.out.println(cancel); bundle = ResourceBundle.getBundle("res", Locale.getDefault()); cancel = bundle.getString("cancelKey"); System.out.println(cancel); bundle = ResourceBundle.getBundle("res", Locale.GERMAN); cancel = bundle.getString("cancelKey"); System.out.println(cancel); } }
輸出:
取消 cancel 取消 取消
這裏前三個都在咱們的預期範圍以內,可是最後一個GERMAN,應該會去使用res.properties這個資源包吧?怎麼使用了res_zh_CH.properties?
原來ResourceBundle爲咱們提供了一個fallback(也就是一個備用方案),這個備用方案就是根據當前系統的語言環境來獲得的本地化信息。
因此如果找不到GERMAN的,以後就會去找CHINA了,因此找到了res_zh_CH.properties這個資源包
這點我也是看了源代碼才明白的,下面就貼上一些關鍵的源代碼:
ResourceBundle baseBundle = null; for (Locale targetLocale = locale; targetLocale != null; targetLocale = control.getFallbackLocale(baseName, targetLocale)) {// 這裏就是去拿備用方案的 // do something 咱們暫時不關心 }
跟蹤control.getFallbackLocale(baseName, targetLocale)看看備用方案究竟是什麼?
public Locale getFallbackLocale(String baseName, Locale locale) { if (baseName == null) { throw new NullPointerException(); } Locale defaultLocale = Locale.getDefault(); return locale.equals(defaultLocale) ? null : defaultLocale; }
--------------Java中getResourceAsStream的用法讀取配置文件----------------------------
首先,Java中的getResourceAsStream有如下幾種:
1. Class.getResourceAsStream(String path) : path 不以’/'開頭時默認是今後類所在的包下取資源,以’/'開頭則是從
ClassPath根下獲取。其只是經過path構造一個絕對路徑,最終仍是由ClassLoader獲取資源。
2. Class.getClassLoader.getResourceAsStream(String path) :默認則是從ClassPath根下獲取,path不能以’/'開頭,最終是由
ClassLoader獲取資源。
3. ServletContext. getResourceAsStream(String path):默認從WebAPP根目錄下取資源,Tomcat下path是否以’/'開頭無所謂,
固然這和具體的容器實現有關。
4. Jsp下的application內置對象就是上面的ServletContext的一種實現。
其次,getResourceAsStream 用法大體有如下幾種:
第一: 要加載的文件和.class文件在同一目錄下,例如:com.x.y 下有類me.class ,同時有資源文件myfile.xml
那麼,應該有以下代碼:
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目錄的子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.y.file 目錄下有資源文件myfile.xml
那麼,應該有以下代碼:
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目錄下,也不在子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.file 目錄下有資源文件myfile.xml
那麼,應該有以下代碼:
me.class.getResourceAsStream("/com/x/file/myfile.xml");
總結一下,可能只是兩種寫法
第一:前面有 「 / 」
「 / 」表明了工程的根目錄,例如工程名叫作myproject,「 / 」表明了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面沒有 「 / 」
表明當前類的目錄
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");