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 ...
随机推荐
- 使用级联分类器实现人脸检测(OpenCV自带的数据)
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- Android给拼接好的Bitmap加上个性化边框
在上一节中将到将若干张图片拼接成为一张图片.但是这种简单的操作往往不能满足实际的需求,有时我们会需要给图片添加上个性化的边框,来更好的展示图片. 下面就讲一下在图片拼接后如何给bitmap添加边框. ...
- Python操作Saltstack
1.代码 # -*- coding:utf-8 -*- import urllib.request import urllib.parse import json class saltAPI(): d ...
- [Dynamics 365] 关于Currency的一点随笔
在Dynamics CRM中,如果我们要添加一条Currency记录的话. 可选择的Currency Type有<System> ,<Custorm>两种. 如果选择的是< ...
- Patchwork(2013年)--CNV检测方法流程
文章题目:Patchwork: allele-specific copy number analysis of whole-genome sequenced tumor tissue 特点: 可以检测 ...
- Keras实现风格迁移
风格迁移 风格迁移算法经历多次定义和更新,现在应用在许多智能手机APP上. 风格迁移在保留目标图片内容的基础上,将图片风格引用在目标图片上. 风格本质上是指在各种空间尺度上图像中的纹理,颜色和视觉图案 ...
- ScreenToGif 代码分析
ScreenToGif项目由四个文件夹组成: Files 存放协议文件 GifRecorder 存放gif编码器代码 ScreenToGif 存放主代码 Other 存放Hooktest和Transl ...
- libgdx学习记录21——Box2d物理引擎之碰撞Contact、冲量Impulse、关节Joint
Box2d中,物体可以接受力(Force).冲量(Impulse)和扭矩(Torque).这些物理元素都能改变物体的运动形式,并且默认都会唤醒物体,当然只是针对动态物体. 力是一个持久的效果,通过Bo ...
- PowerBI开发 第八篇:查询参数
在PowerBI Desktop中,用户可以定义一个或多个查询参数(Query Parameter),参数的功能是为了实现PowerBI的参数化编程,使得Data Source的属性.替换值和过滤数据 ...
- eclipse中怎么找项目部署的路径和找编译后的class路径
1.快捷键 ctrl+shift+R,会默认显示你的源文件.java的路径,如果没有.class的话,点击右上角的三角,选中 Show Derived Resource; 2.打开出现下图 3.按下 ...