查找配置文件的工具类

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
        }
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值