代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
查找配置文件的工具类
作者:
方强
/ 发布于
2016/11/14
/
775
package org.happy.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ConfigUtils { private static final Logger LOG = LoggerFactory.getLogger(IOUtils.class); private IOUtils() { super(); } public static InputStream getInputStream(String path) throws FileNotFoundException{ return getInputStream(path, IOUtils.class); } /** * 查找配置文件: * 1) 从用户工作目录查找; * 2)从用户classes类加载路径查找; * 3)从用户给定的类所在的包里开始查找; * * @param path * @param claz * @return * @throws FileNotFoundException */ public static InputStream getInputStream(String path, Class<?> claz) throws FileNotFoundException{ InputStream is = null; URL clasPath, packPath=null; File file = new File(System.getProperty("user.dir"), path); if(file.exists()){ LOG.info("found file: {}", file.getAbsolutePath()); is = new FileInputStream(file); }else{ // 这里是从classes开始查找 ClassLoader cl = claz.getClassLoader(); clasPath = cl.getResource(path); is = cl.getResourceAsStream(path); if(is != null){ LOG.info("found file: {}", clasPath); }else{ // 这里是从class对应得包开始查找 packPath = claz.getResource(path); is = claz.getResourceAsStream(path); if(is != null){ LOG.info("found file: {}", packPath); }else{ LOG.error("No found file: {}, search path: \n {} \n {} \n {}", path, file, clasPath, packPath); } } } return is; } /** * Borrowed from apache IOUtils; * * Unconditionally close an Reader. * * Equivalent to {@link Reader#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param input the Reader to close, may be null or already closed */ public static void closeQuietly(Reader input) { try { if (input != null) { input.close(); } } catch (IOException ioe) { // ignore } } /** * Unconditionally close an InputStream. * * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param input the InputStream to close, may be null or already closed */ public static void closeQuietly(InputStream input) { try { if (input != null) { input.close(); } } catch (IOException ioe) { // ignore } } /** * Borrowed from apache IOUtils; * * Unconditionally close a Writer. * * Equivalent to {@link Writer#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param output the Writer to close, may be null or already closed */ public static void closeQuietly(Writer output) { try { if (output != null) { output.close(); } } catch (IOException ioe) { // ignore } } /** * Unconditionally close an OutputStream. * * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param output the OutputStream to close, may be null or already closed */ public static void closeQuietly(OutputStream output) { try { if (output != null) { output.close(); } } catch (IOException ioe) { // ignore } } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
方强
贡献的其它代码
(
8
)
.
xml解析利用正则表达式获取指定两个字符串之间内容
.
生成时间+4位随机号
.
查询所有本周,本月的数据
.
解析url获取域
.
剪贴板相关
.
查找配置文件的工具类
.
实现前台标题变色
.
验证码倒计时60秒
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3