Read Resource File

来源:互联网 发布:聊天软件市场 编辑:程序博客网 时间:2024/06/05 14:18

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

 

public class ConvoyKBConstants {
    /**
     *
     * @param proName
     * @return
     */
    public static String loadConfigFile(String proName) {
        return baseLoad("convoykb.properties", proName);
    }
    /**
     *
     * @param fileName
     * @param proName
     * @return
     */
    public static String loadFile(String fileName,String proName){
        return baseLoad(fileName, proName);
    }
    /**
     *
     * @param fileName
     * @param proName
     * @return
     */
    private static String baseLoad(String fileName, String proName) {
        Properties properties;
        String name = getReasourcePath(fileName);
        properties = new Properties();
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(name);
            properties.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
//            log.error("",e);
        } catch (IOException e) {
            e.printStackTrace();
//            log.error("",e);
        }finally{
            try {   
                if(fis!=null)
                  fis.close();
            } catch (IOException e) {               
                e.printStackTrace();
//                log.error("",e);
            }
        }
        return properties.getProperty(proName);
    }
    /**
     *
     * @param resourceName
     * @return
     */
    public static String getReasourcePath(String resourceName) {
//        log.info("loading "+resourceName);       
        if(ConvoyKBConstants.class.getClassLoader().getResource(resourceName)==null) {
//            log.error("Cound not find resource: "+resourceName);
            return resourceName;
        }
        return ConvoyKBConstants.class.getClassLoader().getResource(resourceName).getPath();
    }

原创粉丝点击