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 ...
随机推荐
- Python2.7-decimal
decimal 模块,提供了对小数精确的计算,内置的 float 类型是以二进制的浮点数保存的,是不准确的,小数点后会有很多奇怪的数字,虽然在一般情况下计算是没问题的,因为近似相等,小数点后十几位才会 ...
- Python2.7-weakref
weakref 模块,允许创建对象的弱引用,被弱引用的对象其引用计数不变,对象的引用计数为0时就会被垃圾清理机制释放内存空间,此时对其的弱引用也会失效.在对象会被交叉引用,需要释放内存空间时常用. 模 ...
- CentOS7服务器上部署深度/机器学习环境推荐首选anaconda3
CentOS7服务器上部署深度/机器学习环境推荐首选anaconda3,亲测~~ 因为可以创建不同的环境版本或虚拟环境 CentOS7服务器安装anaconda3后,CentOS7服务器开启后自动将a ...
- js阻止事件冒泡的两种方法
1.什么是JS事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这 ...
- mvn dependency:tree
jar依赖冲突解决实践 前言 随着功能的增多,各种中间件的引入.应用以来的各种jar的规模极具膨胀,出现jar冲突和Class冲突的问题层出不穷,让人不胜其扰.本文针对冲突,提供一个排查和定位问题的最 ...
- 解决php的交互式命令行不能正常启动的问题兼介绍psysh
今天在自己的mac电脑上试着启动php的交互式命令行,发现敲下命令后一直卡在提示进入的地方,但没有出现已经进入的提示符,百度了下应该是与readline有关. 于是安装php的readline扩展,在 ...
- Flutter - JSON to Dart,一个json转dart实体的网站
如你所见,一个json转dart实体的网站,https://javiercbk.github.io/json_to_dart/
- Python基础(条件判断和循环) if elif else for while break continue;
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- 查看Oracle数据库中的,已经连接好的..当前用户状况
参考: http://stackoverflow.com/questions/1043096/how-to-list-active-open-connections-in-oracle 以sys身份连 ...
- HTML基础之CSS
CSS选择器 1.id选择器 2.class选择器 3.标签选择器 4.层级选择器(空格) 5.组合选择器(逗号) 6.属性选择器(中括号) <!DOCTYPE html> <htm ...