文件读取工具类
来源:互联网 发布:网络通信协议图 编辑:程序博客网 时间:2024/06/06 00:41
该工具类是根据shiro工具类进行改写的。可以读取类路径下的文件、文件系统文件和网络文件。
maven
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency>
package com.utils.io;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;/** * Created by Administrator on 2017-05-13. */public class ResourceUtils { private static final Logger log = LoggerFactory.getLogger(ResourceUtils.class); public static final String CLASSPATH_PREFIX = "classpath:"; public static final String URL_PREFIX = "url:"; public static final String FILE_PREFIX = "file:"; private ResourceUtils() { } public static boolean hasResourcePrefix(String resourcePath) { return resourcePath != null && (resourcePath.startsWith("classpath:") || resourcePath.startsWith("url:") || resourcePath.startsWith("file:")); } public static boolean resourceExists(String resourcePath) { InputStream stream = null; boolean exists = false; try { stream = getInputStreamForPath(resourcePath); exists = true; } catch (IOException var12) { stream = null; } finally { if (stream != null) { try { stream.close(); } catch (IOException var11) { ; } } } return exists; } public static InputStream getInputStreamForPath(String resourcePath) throws IOException { InputStream is; if (resourcePath.startsWith("classpath:")) { is = loadFromClassPath(stripPrefix(resourcePath)); } else if (resourcePath.startsWith("url:")) { is = loadFromUrl(stripPrefix(resourcePath)); } else if (resourcePath.startsWith("file:")) { is = loadFromFile(stripPrefix(resourcePath)); } else { is = loadFromFile(resourcePath); } if (is == null) { throw new IOException("Resource [" + resourcePath + "] could not be found."); } else { return is; } } private static InputStream loadFromFile(String path) throws IOException { if (log.isDebugEnabled()) { log.debug("Opening file [" + path + "]..."); } return new FileInputStream(path); } private static InputStream loadFromUrl(String urlPath) throws IOException { log.debug("Opening url {}", urlPath); URL url = new URL(urlPath); return url.openStream(); } private static InputStream loadFromClassPath(String path) { log.debug("Opening resource from class path [{}]", path); return ResourcesUtilsBase.getResourceAsStream(path); } private static String stripPrefix(String resourcePath) { return resourcePath.substring(resourcePath.indexOf(":") + 1); } public static void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException var2) { log.warn("Error closing input stream.", var2); } } }}
package com.utils.io;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.InputStream; class ResourcesUtilsBase { private static final Logger log = LoggerFactory.getLogger(ResourcesUtilsBase.class); private static final ResourcesUtilsBase.ClassLoaderAccessor THREAD_CL_ACCESSOR = new ResourcesUtilsBase.ExceptionIgnoringAccessor() { protected ClassLoader doGetClassLoader() throws Throwable { return Thread.currentThread().getContextClassLoader(); } }; private static final ResourcesUtilsBase.ClassLoaderAccessor CLASS_CL_ACCESSOR = new ResourcesUtilsBase.ExceptionIgnoringAccessor() { protected ClassLoader doGetClassLoader() throws Throwable { return ResourcesUtilsBase.class.getClassLoader(); } }; private static final ResourcesUtilsBase.ClassLoaderAccessor SYSTEM_CL_ACCESSOR = new ResourcesUtilsBase.ExceptionIgnoringAccessor() { protected ClassLoader doGetClassLoader() throws Throwable { return ClassLoader.getSystemClassLoader(); } }; public ResourcesUtilsBase() { } /** *从类路径下获取资源 * @param name 类路径。如:config/jdbc.properties * @return */ public static InputStream getResourceAsStream(String name) { InputStream is = THREAD_CL_ACCESSOR.getResourceStream(name); if(is == null) { if(log.isTraceEnabled()) { log.trace("Resource [" + name + "] was not found via the thread context ClassLoader. Trying the " + "current ClassLoader..."); } is = CLASS_CL_ACCESSOR.getResourceStream(name); } if(is == null) { if(log.isTraceEnabled()) { log.trace("Resource [" + name + "] was not found via the current class loader. Trying the " + "system/application ClassLoader..."); } is = SYSTEM_CL_ACCESSOR.getResourceStream(name); } if(is == null && log.isTraceEnabled()) { log.trace("Resource [" + name + "] was not found via the thread context, current, or " + "system/application ClassLoaders. All heuristics have been exhausted. Returning null."); } return is; } private abstract static class ExceptionIgnoringAccessor implements ResourcesUtilsBase.ClassLoaderAccessor { private ExceptionIgnoringAccessor() { } public InputStream getResourceStream(String name) { InputStream is = null; ClassLoader cl = this.getClassLoader(); if(cl != null) { is = cl.getResourceAsStream(name); } return is; } protected final ClassLoader getClassLoader() { try { return this.doGetClassLoader(); } catch (Throwable var2) { if(ResourcesUtilsBase.log.isDebugEnabled()) { ResourcesUtilsBase.log.debug("Unable to acquire ClassLoader.", var2); } return null; } } protected abstract ClassLoader doGetClassLoader() throws Throwable; } private interface ClassLoaderAccessor { InputStream getResourceStream(String var1); }}
0 0
- 文件读取工具类
- xml文件读取工具类
- Properties文件读取工具类
- 读取properties文件,工具类。
- SFTP文件读取工具类
- Properties文件读取工具类
- Properties文件读取工具类
- 属性文件读取工具类
- 属性文件读取工具类
- java读取文件工具类
- 读取properties文件工具类
- properties文件读取工具类
- 读取properties文件的工具类
- java读取Properties文件的工具类
- 读取资源文件的工具类.
- Cocos2d-x读取Csv文件工具类
- Properties属性文件载入、读取工具类
- Java读取属性文件简洁工具类
- 【转】python--pyqt窗体背景透明的两种应用
- [模板]
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) B. T-shirt buying
- 8 多态
- 9 接口
- 文件读取工具类
- According to TLD or attribute directive in tag file, attribute var does not accept any expressions
- #AR游戏--音之国度#初次简单的测试
- 八种排序算法(直接插入,希尔,直接选择,堆,冒泡,快排,归并,基数)
- 10 内部类
- 软件工程-2.软件可行性研究
- 11 持有对象
- Retrofit2.0和它的拦截器们
- OSI七层模型详解