Java之资源文件读取
ClassLoaderWrapper.java
package org.utils.resource; import java.io.InputStream;
import java.net.URL; class ClassLoaderWrapper { ClassLoader defaultClassLoader;
ClassLoader systemClassLoader; ClassLoaderWrapper() {
try {
systemClassLoader = ClassLoader.getSystemClassLoader();
}
catch (SecurityException ignored) {}
} public URL getResourceAsURL(String resource) {
return getResourceAsURL(resource, getClassLoaders(null));
} public URL getResourceAsURL(String resource, ClassLoader classLoader) {
return getResourceAsURL(resource, getClassLoaders(classLoader));
} public InputStream getResourceAsStream(String resource) {
return getResourceAsStream(resource, getClassLoaders(null));
} public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
return getResourceAsStream(resource, getClassLoaders(classLoader));
} public Class<?> classForName(String name) throws ClassNotFoundException {
return classForName(name, getClassLoaders(null));
} public Class<?> classForName(String name, ClassLoader classLoader) throws ClassNotFoundException {
return classForName(name, getClassLoaders(classLoader));
} InputStream getResourceAsStream(String resource, ClassLoader[] classLoaderArray) {
for (ClassLoader classLoader : classLoaderArray) {
if (null != classLoader) {
InputStream returnValue = classLoader.getResourceAsStream(resource);
if (null == returnValue) returnValue = classLoader.getResourceAsStream("/" + resource);
if (null != returnValue) return returnValue;
}
} return null;
} URL getResourceAsURL(String resource, ClassLoader[] classLoaderArray) {
URL url; for (ClassLoader classLoader : classLoaderArray) {
if (null != classLoader) {
url = classLoader.getResource(resource);
if (null == url) url = classLoader.getResource("/" + resource);
if (null != url) return url;
}
} return null;
} Class<?> classForName(String name, ClassLoader[] classLoaderArray) throws ClassNotFoundException {
for (ClassLoader classLoader : classLoaderArray) {
if (null != classLoader) {
try {
Class<?> clazz = Class.forName(name, true, classLoader);
if (null != clazz) return clazz;
}
catch (ClassNotFoundException e) {}
}
} throw new ClassNotFoundException("Cannot find class: " + name);
} ClassLoader[] getClassLoaders(ClassLoader classLoader) {
return new ClassLoader[] {
classLoader,
defaultClassLoader,
Thread.currentThread().getContextClassLoader(),
getClass().getClassLoader(),
systemClassLoader
};
}
}
ResourcesUtils.java
package org.utils.resource; import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Properties; public class ResourcesUtils { private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper(); private static Charset charset; public static ClassLoader getDefaultClassLoader() {
return classLoaderWrapper.defaultClassLoader;
} public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
classLoaderWrapper.defaultClassLoader = defaultClassLoader;
} public static Charset getCharset() {
return charset;
} public static void setCharset(Charset charset) {
ResourcesUtils.charset = charset;
} public static URL getResourceURL(String resource) throws IOException {
return getResourceURL(null, resource);
} public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
if (url == null) throw new IOException("Could not find resource " + resource); return url;
} public static InputStream getResourceAsStream(String resource) throws IOException {
return getResourceAsStream(null, resource);
} public static InputStream getResourceAsStream(ClassLoader loader, String resource)
throws IOException {
InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) throw new IOException("Could not find resource " + resource); return in;
} public static Properties getResourceAsProperties(String resource) throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(resource);
props.load(in);
in.close(); return props;
} public static Properties getResourceAsProperties(ClassLoader loader, String resource)
throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(loader, resource);
props.load(in);
in.close(); return props;
} public static Properties getResourceAsProperties(File file) throws IOException {
Properties props = new Properties();
if (!file.exists()) return null;
InputStream in = new FileInputStream(file);
props.load(in);
in.close(); return props;
} public static Reader getResourceAsReader(String resource) throws IOException {
Reader reader;
if (charset == null) reader = new InputStreamReader(getResourceAsStream(resource));
else reader = new InputStreamReader(getResourceAsStream(resource), charset); return reader;
} public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
Reader reader;
if (charset == null) reader = new InputStreamReader(getResourceAsStream(loader, resource));
else reader = new InputStreamReader(getResourceAsStream(loader, resource), charset); return reader;
} public static File getResourceAsFile(String resource) throws IOException, URISyntaxException {
return new File(getResourceURL(resource).toURI());
} public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException
, URISyntaxException {
return new File(getResourceURL(loader, resource).toURI());
} public static InputStream getUrlAsStream(String urlString) throws IOException {
URL url = new URL(urlString);
URLConnection conn = url.openConnection(); return conn.getInputStream();
} public static Reader getUrlAsReader(String urlString) throws IOException {
Reader reader;
if (charset == null) reader = new InputStreamReader(getUrlAsStream(urlString));
else reader = new InputStreamReader(getUrlAsStream(urlString), charset); return reader;
} public static Properties getUrlAsProperties(String urlString) throws IOException {
Properties props = new Properties();
InputStream in = getUrlAsStream(urlString);
props.load(in);
in.close(); return props;
} public static Class<?> classForName(String className) throws ClassNotFoundException {
return classLoaderWrapper.classForName(className);
}
}
Java之资源文件读取的更多相关文章
- java web 资源文件读取
前提:假设web应用test(工程名) webapps下面有一资源文件test.html 规则:在获取资源时一般使用的是相对路径,以符号/开头,而 / 代表什么取决于这个地址给谁使用.服务器使用时,/ ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Java学习-019-Properties 文件读取实例源代码
在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...
- Java学习-017-EXCEL 文件读取实例源代码
众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...
- Java学习-016-CSV 文件读取实例源代码
上文(CSV文件写入)讲述了日常自动化测试过程中将测试数据写入 CSV 文件的源码,此文主要讲述如何从 CSV 文件获取测试过程中所需的参数化数据.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感 ...
- Eclipse中建立Maven项目后,Java Resources资源文件下没有src/main/java文件夹
当建立好一个Maven项目后,在Java Resources资源文件夹下没有看到src/main/java文件夹,然后手动去创建Source Folder时,提示该文件已存在,如图: 有一个解决办法: ...
- Java解决大文件读取的内存问题以及文件流的比较
Java解决大文件读取的内存问题以及文件流的比较 传统方式 读取文件的方式一般是是从内存中读取,官方提供了几种方式,如BufferedReader, 以及InputStream 系列的,也有封装好的如 ...
- Java 从资源文件(.properties)中读取数据
在Java工程目录src下,创建一个后缀为.properties的文件,例如db.properties 文件中的内容如下(键=值): name=mk age=123 address=China 在程序 ...
- Java_web项目中在Java文件里面通过类装载器对资源文件读取
承接上一节:在eclipse完成对Java_web项目里面资源文件的读取 我们首先在src目录下创建一个资源文件db.properties 内容如下: url=127.0.0.1 name=root ...
随机推荐
- 各种 mv power cell
1. retention register : VDDB 是 backup power,当primary power shutoff 时 backup 会继续供电,将 reg 当前状态保存下来 2. ...
- JS 仿腾讯发表微博的效果
JS 仿腾讯发表微博的效果 最近2天研究了下 腾讯发表微博的效果 特此来分享下,效果如下: 在此分享前 来谈谈本人编写代码的习惯,很多人会问我既然用的是jquery框架 为什么写的组件不用Jquery ...
- 备份时如何排除掉默认的 information_schema 和 mysql 库?
备份时如何排除掉默认的 information_schema 和 mysql 库? mysql -e "show databases;" -uroot -ppassword | g ...
- 在mvc视图中实现rdlc报表展示
需求:在view视图页面中嵌入rdlc报表,rdlc的xml为动态传入的xml字符串.本项目是基于abp框架 可能出现问题: 1.rdlc报表是由asp.net的服务器控件ReportViewer来支 ...
- jqgrid 设置多表头
有时,我们需要给jqgrid设置多表头信息,多表头区域会有行合并/列合并,如何实现? 1)通过jqgrid的 setGroupHeaders 方法来实现一个行的多表头, 2)如果有多行表头,需要设置多 ...
- 深入解析Java中的装箱和拆箱
自己主动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最主要的东西,再来看一以下试笔试中常常遇到的与装箱.拆箱相关的问题. 下面是本 ...
- 自研后端HTTP请求参数验证器服务ParamertValidateService
好处:方便了后端对HTTP请求中参数进行核验,只需一次编写效验器,一行代码便可对所有参数的pojo进行参数核验!而且更改效验逻辑时只需要更改效验器类即可,实现了解耦合. 只需要程序员按照规范开发一个P ...
- WPF 嵌入winform 控件
引入 WindowsFormsIntegration.dll 和 System.Windows.Forms.dll <Window x:Class="wgscd.Window1 ...
- 20155305乔磊《网络对抗》逆向及Bof基础
20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...
- 12、JAVA内存模型与线程
一.JMM 有序性,可见性,原子性 synchorize :3个性都有: volatile:保证可见性+禁止指令重排: 二.线程的五种状态 面向过程与面向对象的差别 面向过程:站在计算机的角度分析和解 ...