类加载器使用工具类

类加载器使用工具类


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 类加载器使用工具类
 * 
 * @author wangwp
 * @version $Id: ClassLoaderUtil.java,v 1.2 2008/09/10 08:52:25 jabberdev Exp $
 */
public abstract class ClassLoaderUtil {
    private static final Log logger = LogFactory.getLog(ClassLoaderUtil.class);
    private static final Properties EMPTY_PROPERTIES = new Properties();

    /**
     * 获得资源所在真实文件路径
     * 
     * @param resource
     *                资源
     * @return
     */
    public static String getPath(String resource) {
return getURL(resource).getPath();
    }

    public static URL getURL(String resource) {
return getClassLoader().getResource(resource);
    }

    /**
     * 创建指定类的实例
     * 
     * @param clazzName 类名
     * @return
     */
    public static Object getInstance(String clazzName){
try {
   return loadClass(clazzName).newInstance();
} catch (InstantiationException e) {
   e.printStackTrace();
} catch (IllegalAccessException e) {
   e.printStackTrace();
}
return null;
    }
    /**
     * 根据指定的类名加载类
     * 
     * @param name
     *                类名
     * @return
     */
    public static Class<?> loadClass(String name) {
try {
   ClassLoaderUtil.class.getClassLoader().loadClass(name);
   return getClassLoader().loadClass(name);

} catch (ClassNotFoundException ex) {
   throw new RuntimeException(ex);
}
    }

    public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();

    }

    /**
     * 将资源文件加载到输入流中
     * 
     * @param resource
     *                资源文件
     * @return
     */
    public static InputStream getStream(String resource) {
return getClassLoader().getResourceAsStream(resource);
    }

    /**
     * 将资源文件转化为Properties对象
     * 
     * @param resource
     *                资源文件
     * @return
     */
    public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
   InputStream is = getStream(resource);
   if (is == null)
return EMPTY_PROPERTIES;
   properties.load(is);
   return properties;
} catch (IOException ex) {
   return EMPTY_PROPERTIES;
}
    }

    /**
     * 将资源文件转化为Reader
     * 
     * @param resource
     *                资源文件
     * @return
     */
    public static Reader getReader(String resource) {
InputStream is = getStream(resource);
if (is == null) {
   return null;
}
return new BufferedReader(new InputStreamReader(is));
    }

    /**
     * 将资源文件的内容转化为List实例
     * 
     * @param resource
     *                资源文件
     * @return
     */
    public static List<String> getList(String resource) {
List<String> list = new ArrayList<String>();
BufferedReader reader = (BufferedReader) getReader(resource);
if (null == reader) {
   return list;
}
String line = "";
try {
   while ((line = reader.readLine()) != null) {
list.add(line);
   }
} catch (IOException ex) {
   logger.error("将资源文件转化为list出现异常", ex);
} finally {
   if (reader != null)
try {
   reader.close();
} catch (IOException ex) {
  logger.warn(ex.getMessage());
}
}
return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值