不同的开发者对资源文件的管理千差万别,有的喜欢把它们放在不同的类路径下面,有的喜欢放在单一的config文件下,没有一种方法是错误的。但是为了能够正确的加载到你需要的资源文件,永远应该记得把资源文件夹的路径配置做为classpath的一部分,如果资源文件没有位于你的source folder之内。否则你不得不通过额外的手段来配置与加载你的资源文件。
基于上述的总结,完成了一个资源加载的UtilityClass, 让你从此不再对Java中资源加载怀有好奇与恐惧之心。
Resource Loading Utility Class源代码如下:
- import java.io.InputStream;
- import java.util.Locale;
- import java.util.Properties;
- import java.util.ResourceBundle;
- public class CommonResourceLoader {
- /**
- * please use this method if property files located at same package with CommonResourceLoader
- * Java class
- *
- * @param PropertiesName
- * @return
- */
- public static Properties loadPropertyFile(String PropertiesName) {
- Properties props = new Properties();
- try
- {
- InputStream in = CommonResourceLoader.class.getResourceAsStream(PropertiesName);
- if (in != null)
- {
- props.load(in);
- }
- else
- {
- throw new IllegalArgumentException("No file exists for" + PropertiesName);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return props;
- }
- /**
- * if the properties file is located at obj's package, please use it
- * @param obj
- * @param PropertiesName
- * @return
- */
- public static Properties loadPropertyFile(Class obj, String PropertiesName) {
- Properties props = new Properties();
- try
- {
- InputStream in = obj.getResourceAsStream(PropertiesName);
- if (in != null)
- {
- props.load(in);
- }
- else
- {
- throw new IllegalArgumentException("No file exists for" + PropertiesName);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return props;
- }
- /**
- * base name is file name if file located at class path root directory
- * base name is directory + file full name if file at some folder inside class path list.
- * eg: myPro.properties
- * project structure
- * pro--src
- * | |---com.mytest.pkg
- * | | |-----Hello.java
- * | | |-----test.properties
- * |---myPro.properties
- * baseName= com/mytest/pkg/test.properties if load test.properties
- * baseName= myPro.properties if load myPro.properties
- *
- * @param baseName
- * @return
- */
- public static Properties loadCurrentPropertyFile(String baseName) {
- Properties props = new Properties();
- try
- {
- InputStream in = CommonResourceLoader.class.getClassLoader().getResourceAsStream(baseName);
- if (in != null)
- {
- props.load(in);
- }
- else
- {
- throw new IllegalArgumentException("No file exists for" + baseName);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return props;
- }
- public static Properties loadSystemPropertyFile(String name, ClassLoader loader) {
- Properties props = new Properties();
- try
- {
- if(loader == null)
- loader = ClassLoader.getSystemClassLoader();
- InputStream in = loader.getResourceAsStream(name);
- if (in != null)
- {
- props.load(in);
- }
- else
- {
- throw new IllegalArgumentException("No file exists for" + name);
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return props;
- }
- /**
- * resourceURL is file name if file located at class path root directory
- * resourceURL is directory + file full name if file at some folder inside class path list.
- * eg: myPro.properties
- * project structure
- * pro--src
- * | |---com.mytest.pkg
- * | | |-----Hello.java
- * | | |-----test.properties
- * |---myPro.properties
- * resourceURL= com.mytest.pkg.test.properties if load test.properties
- * resourceURL= myPro.properties if load myPro.properties
- *
- * @param baseName
- * @return
- */
- public static ResourceBundle loadResourceBundle(String resourceURL) {
- Locale locale = Locale.ENGLISH;
- ResourceBundle bundle = null;
- try {
- bundle = ResourceBundle.getBundle(resourceURL, locale);
- if (bundle == null) {
- throw new IllegalArgumentException("No file exists for" + resourceURL);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return bundle;
- }
- }
这里的资源是指配置文件(xml,property等), 图片,Java程序运行需要的一切文本文件等等。
