手机版
你好,游客 登录 注册
背景:
阅读新闻

走出Java资源加载的迷宫

[日期:2012-04-29] 来源:Linux社区  作者:jia20003 [字体:大 中 小]
资源加载策略说明:

不同的开发者对资源文件的管理千差万别,有的喜欢把它们放在不同的类路径下面,有的喜欢放在单一的config文件下,没有一种方法是错误的。但是为了能够正确的加载到你需要的资源文件,永远应该记得把资源文件夹的路径配置做为classpath的一部分,如果资源文件没有位于你的source folder之内。否则你不得不通过额外的手段来配置与加载你的资源文件。

基于上述的总结,完成了一个资源加载的UtilityClass, 让你从此不再对Java中资源加载怀有好奇与恐惧之心。

Resource Loading Utility Class源代码如下:

  1. import java.io.InputStream;  
  2. import java.util.Locale;  
  3. import java.util.Properties;  
  4. import java.util.ResourceBundle;  
  5.   
  6. public class CommonResourceLoader {  
  7.   
  8.         /** 
  9.          * please use this method if property files located at same package with CommonResourceLoader 
  10.          * Java class 
  11.          *  
  12.          * @param PropertiesName 
  13.          * @return 
  14.          */  
  15.         public static Properties loadPropertyFile(String PropertiesName) {  
  16.   
  17.               Properties props = new Properties();  
  18.   
  19.               try  
  20.               {  
  21.                   InputStream in = CommonResourceLoader.class.getResourceAsStream(PropertiesName);  
  22.                  if (in != null)  
  23.                  {  
  24.                     props.load(in);  
  25.                  }   
  26.                  else  
  27.                  {  
  28.                     throw new IllegalArgumentException("No file exists for" + PropertiesName);  
  29.                  }  
  30.               }  
  31.               catch (Exception e)  
  32.               {  
  33.                  e.printStackTrace();  
  34.               }  
  35.   
  36.               return props;  
  37.         }  
  38.           
  39.         /** 
  40.          * if the properties file is located at obj's package, please use it 
  41.          * @param obj 
  42.          * @param PropertiesName 
  43.          * @return 
  44.          */  
  45.         public static Properties loadPropertyFile(Class obj,  String PropertiesName) {  
  46.   
  47.               Properties props = new Properties();  
  48.   
  49.               try  
  50.               {  
  51.                   InputStream in = obj.getResourceAsStream(PropertiesName);  
  52.                  if (in != null)  
  53.                  {  
  54.                     props.load(in);  
  55.                  }   
  56.                  else  
  57.                  {  
  58.                     throw new IllegalArgumentException("No file exists for" + PropertiesName);  
  59.                  }  
  60.               }  
  61.               catch (Exception e)  
  62.               {  
  63.                  e.printStackTrace();  
  64.               }  
  65.   
  66.               return props;  
  67.         }  
  68.           
  69.         /** 
  70.          * base name is file name if file located at class path root directory 
  71.          * base name is directory + file full name if file at some folder inside class path list. 
  72.          * eg: myPro.properties 
  73.          * project structure 
  74.          * pro--src 
  75.          *  |    |---com.mytest.pkg 
  76.          *  |    |               |-----Hello.java 
  77.          *  |    |               |-----test.properties 
  78.          *  |---myPro.properties 
  79.          *  baseName= com/mytest/pkg/test.properties if load test.properties 
  80.          *  baseName= myPro.properties if load myPro.properties 
  81.          * 
  82.          * @param baseName 
  83.          * @return 
  84.          */  
  85.         public static Properties loadCurrentPropertyFile(String baseName) {  
  86.             Properties props = new Properties();  
  87.   
  88.             try  
  89.             {  
  90.                 InputStream in = CommonResourceLoader.class.getClassLoader().getResourceAsStream(baseName);  
  91.                if (in != null)  
  92.                {  
  93.                   props.load(in);  
  94.                }   
  95.                else  
  96.                {  
  97.                   throw new IllegalArgumentException("No file exists for" + baseName);  
  98.                }  
  99.             }  
  100.             catch (Exception e)  
  101.             {  
  102.                e.printStackTrace();  
  103.             }  
  104.   
  105.             return props;  
  106.         }  
  107.           
  108.         public static Properties loadSystemPropertyFile(String name, ClassLoader loader) {  
  109.             Properties props = new Properties();  
  110.   
  111.             try  
  112.             {  
  113.                 if(loader == null)  
  114.                     loader = ClassLoader.getSystemClassLoader();  
  115.                 InputStream in = loader.getResourceAsStream(name);  
  116.                if (in != null)  
  117.                {  
  118.                   props.load(in);  
  119.                }   
  120.                else  
  121.                {  
  122.                   throw new IllegalArgumentException("No file exists for" + name);  
  123.                }  
  124.             }  
  125.             catch (Exception e)  
  126.             {  
  127.                e.printStackTrace();  
  128.             }  
  129.   
  130.             return props;  
  131.         }  
  132.           
  133.         /** 
  134.          * resourceURL is file name if file located at class path root directory 
  135.          * resourceURL is directory + file full name if file at some folder inside class path list. 
  136.          * eg: myPro.properties 
  137.          * project structure 
  138.          * pro--src 
  139.          *  |    |---com.mytest.pkg 
  140.          *  |    |               |-----Hello.java 
  141.          *  |    |               |-----test.properties 
  142.          *  |---myPro.properties 
  143.          *  resourceURL= com.mytest.pkg.test.properties if load test.properties 
  144.          *  resourceURL= myPro.properties if load myPro.properties 
  145.          * 
  146.          * @param baseName 
  147.          * @return 
  148.          */  
  149.         public static ResourceBundle loadResourceBundle(String resourceURL) {  
  150.   
  151.                 Locale locale = Locale.ENGLISH;  
  152.                 ResourceBundle bundle = null;  
  153.   
  154.                 try {  
  155.                         bundle = ResourceBundle.getBundle(resourceURL, locale);  
  156.                         if (bundle == null) {  
  157.                                 throw new IllegalArgumentException("No file exists for" + resourceURL);  
  158.                         }  
  159.                 } catch (Exception e) {  
  160.                         e.printStackTrace();  
  161.                 }  
  162.   
  163.                 return bundle;  
  164.         }  
  165.   
  166. }  
特别说明:

这里的资源是指配置文件(xml,property等), 图片,Java程序运行需要的一切文本文件等等。

linux
相关资讯       Java教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款