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 <code>Reader</code>.
* <p>
* 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 <code>InputStream</code>.
* <p>
* 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 <code>Writer</code>.
* <p>
* 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 <code>OutputStream</code>.
* <p>
* 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
}
}
}
查找配置文件的工具类
最新推荐文章于 2021-10-28 23:16:27 发布